我需要调用一个WCF服务。该WCF服务已经启动并且我可以编辑它的配置。
我想创建一个客户端来调用该服务。但是我无法在客户端添加服务引用,因此我正在尝试使用HttpClient来调用它。
客户端代码如下:
using (var client = new HttpClient())
{
//soapString is my input class serialized
var content = new StringContent(soapString, Encoding.UTF8, "text/xml");
using (var postResponse = client.PostAsync("http://localhost:52937/Attempts.svc/", content).Result)
{
string postResult = postResponse.Content.ReadAsStringAsync().Result;
}
}
服务器端代码:
[ServiceContract]
public interface IAttempts
{
[OperationContract]
void ReceiveAttemptResult(ReceiveAttemptResultInput result);
}
public class Attempts : IAttempts
{
string _backendUrl;
public void ReceiveAttemptResult(ReceiveAttemptResultInput result)
{
//...
}
}
最后,在服务器端进行web.config配置:
<system.serviceModel>
<services>
<service name="it.MC.Listeners.Attempts">
<endpoint address="" contract="it.MC.Listeners.IAttempts" binding="basicHttpBinding"/>
<endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
当我调用该服务时,我只得到了一个空字符串,并且无法在服务内部进行调试... 这是怎么回事呢?
谢谢。
http://localhost:52937/Attempts.svc/Endpoint
。 - Oluwafemihttp://localhost:52937/PaymentAttempts.svc/ReceiveAttemptResult
,但仍然没有任何变化:( - SimonehttpClient.DefaultRequestHeaders.Add("SOAPAction", "TheSameAsIn_WcfTestClient")
并且它工作正常! - Disappointed