[英]How to access one WCF service from another?
Actually I am having two service, Like Service A and Service B. I am trying to call Service A to Service B (like you said I got a proxy classes and calling). 实际上,我有两个服务,例如Service A和ServiceB。我试图将Service A调用到Service B(就像您说的那样,我有一个代理类并在调用)。 Now the problem is when I access my Service A from a "postman rest client packaged app" or "Mobile" it is not working.
现在的问题是,当我从“邮递员剩余客户端打包的应用程序”或“移动设备”访问我的服务A时,它不起作用。 Actually From Client("postman rest client packaged app" or "Mobile") Service A is calling fine but Service A can't call Service B.(The remote server returned an unexpected response: (400) Bad Request).
实际上是从客户端(“邮递员剩余客户端打包的应用程序”或“移动”),服务A调用正常,但服务A无法调用服务B。(远程服务器返回了意外响应:(400)错误请求)。 But if I try to access Service A using dll in a console application and calling the Service A. That time Service A can call Service B working fine.
但是,如果我尝试在控制台应用程序中使用dll访问服务A并调用服务A。那次服务A可以正常调用服务B。
Service A:
[ServiceContract]
public interface IUserProfileFacedService
{
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetResult")]
String GetResult(Int64 UserId,String str1,String str2,String str3);
}
public class ServiceA : BaseFacedService, IServiceA
{
public String GetResult(Int64 UserId,String str1,String str2,String str3)
{
ChannelFactory<IService> factory = new ChannelFactory<IService>("webHttpBinding_ServiceReference2");
IService client = factory.CreateChannel();
String Responce = client.GetResult(UserId);
return Responce;
}
}
[ServiceContract]
public interface IServiceB
{
String GetResult(Int64 UserId,String str1,String str2,String str3);
}
public class ServiceB
{
public String GetResult(Int64 UserId,String str1,String str2,String str3)
{
throw new NotImplementedException();
}
}
------------------------
Service B:
[ServiceContract]
public interface IServiceB
{
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetResult")]
[OperationContract]
String GetResult(Int64 UserId,String str1,String str2,String str3);
}
public class ServiceB : BaseFacedService, IServiceB
{
public String GetResult(Int64 UserId,String str1,String str2,String str3)
{
//Some of my code
return Responce;
}
}
Service A WebConfig:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="Hi.Hello.Service.UserProfileFacedService">
<endpoint address="" behaviorConfiguration="restBehav" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="Hi.Hello.Service.IServiceB">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/Hi.Hello.Service./Facade1/" />
</baseAddresses>
</host>
</service>
<service name="Hi.Hello.Service.GridConfigFacedService">
<endpoint address="" behaviorConfiguration="Hi.Hello.Service.GridConfigFacedServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="Hi.Hello.Service.GridConfigFacedService" />
</service>
</services>
<client>
<endpoint address="my client address" binding="webHttpBinding" bindingConfiguration="webHttpBinding_ServiceReference1" behaviorConfiguration="webhttp" contract="Hi.Hello.Service.ServiceB" name="webHttpBinding_ServiceReference2" />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="restBehav">
<webHttp helpEnabled="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
<behavior name="Hi.Hello.Service.GridConfigFacedServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
<behavior name="webhttp">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="netTcpBinding_ServiceReference1" />
</netTcpBinding>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" maxReceivedMessageSize="2147483647" />
<binding name="webHttpBinding_ServiceReference1" />
</webHttpBinding>
</bindings>
</system.serviceModel>
Service B Webconfig
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="Hi.Hello.Registry.ServiceB">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Design_Time_Addresses/Hi.Hello.Service/Facade/"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" contract="Hi.Hello.Registry.IServiceB" behaviorConfiguration="restBehav" bindingConfiguration="webHttpBindingWithJsonP">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!--<endpoint address="" binding="netTcpBinding" contract="Hi.Hello.Registry.IServiceB" bindingConfiguration="netTcpBinding_name">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>-->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restBehav">
<webHttp helpEnabled="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true"/>
</webHttpBinding>
</bindings>
</system.serviceModel>
Fundamentally, you will call a web service the same way, whether it is written in .Net with WCF, WebAPI or some other way, or if the web service is written without .Net. 从根本上讲,无论是以WCF,WebAPI或其他方式在.Net中编写Web服务,还是以没有.Net的方式编写Web服务,您都将以相同的方式调用Web服务。 You must make an HTTP request to a URI.
您必须对URI发出HTTP请求。
This does not vary depending on where you are calling from. 这取决于您从哪里打电话。 All web services are called with HTTP(S) requests, that's what makes them web services.
所有Web服务都通过HTTP(S)请求进行调用,这就是使它们成为Web服务的原因。
There are several simple mechanisms for making web requests with .Net, including, 有几种使用.Net进行Web请求的简单机制,包括:
System.Net.WebClient System.Net.WebClient
and the newer 和更新的
System.Net.Http.HttpClient System.Net.Http.HttpClient
or, you can try and use the WCF client approach expanded here , but I would suggest that this is now outdated. 或者,您可以尝试使用此处扩展的WCF客户端方法 ,但是我建议这已经过时了。 The web service world went RestFul, both for WCF and the simpler and more productive WebAPI .
对于WCF和更简单,更高效的WebAPI来说,Web服务世界都进入了RestFul 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.