Reputation: 302
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). Now the problem is when I access my Service A from a "postman rest client packaged app" or "Mobile" it is not working. 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). 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.
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>
Upvotes: 0
Views: 3550
Reputation: 35716
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. You must make an HTTP request to a URI.
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.
There are several simple mechanisms for making web requests with .Net, including,
and the newer
or, you can try and use the WCF client approach expanded here, but I would suggest that this is now outdated. The web service world went RestFul, both for WCF and the simpler and more productive WebAPI.
Upvotes: 1