如何在asp.net的Windows服务中调用WCF RESTful服务? [英] How to invoke WCF RESTful Service in Windows Service in asp.net?
问题描述
我已经在WCF RESTfule服务中为POST方法创建了以下操作合同.
I have created below Operation Contract for POST Method in WCF RESTfule Service.
IService1.cs:-
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "/SaveCustomerPost",
BodyStyle = WebMessageBodyStyle.Wrapped)]
string SaveCustomerDetails(CustomerDetails objCustomerDetails);
[DataContract]
public class CustomerDetails
{
[DataMember]
public string Name { get; set; }
}
Windows服务:-
using (WebChannelFactory<ServiceReference1.IService1> cf = new WebChannelFactory<ServiceReference1.IService1>(new Uri("http://xxx/CustomerService.svc/SaveCustomerPost")))
{
var helloService = cf.CreateChannel();
ServiceReference1.CustomerDetails objCustomerDetails = new ServiceReference1.PANNoDetails();
objPANNoDetails.Name = "TestName";
string strResult = helloService.SaveCustomerDetails(objPANNoDetails);
}
客户端App.config:-
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="CustBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="WebHttpBinding" sendTimeout="00:05:00" maxBufferSize="2147483647"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
transferMode="Streamed">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<client>
<endpoint address=""
binding="webHttpBinding" behaviorConfiguration="CustBehavior"
contract="ServiceReference1.ICustService" name="WebHttpBinding" />
</client>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0"/>
</system.serviceModel>
在"xxx.svc/SaveCustomerDetails"上没有侦听终结点的端点可以接受该消息.这通常是由不正确的地址或SOAP操作引起的.有关更多详细信息,请参见InnerException(如果存在).
当我在窗口服务中使用WebInvoke方法调用上述方法时,出现了上述错误.当我在没有WebInvoke方法的情况下调用上述服务 时,该服务运行正常.如何解决上述问题?
When I invoke above mentioned method with WebInvoke method in window service I got above mentioned error. When I invoke above mentioned service without WebInvoke method, service is working fine. How to resolve above mentioned issue?
推荐答案
在创建 ChannelFactory
时,尝试仅指定服务名称:
When creating the ChannelFactory
, try to specify the service name only:
using (ChannelFactory<ServiceReference1.ICustService> factory = new ChannelFactory<ServiceReference1.ICustService>(
new WebHttpBinding(),
"http://xxx/CustomerService.svc"))
{
}
这篇关于如何在asp.net的Windows服务中调用WCF RESTful服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!