如何从另一个WCF服务访问?

时间:2015-07-02 08:18:17

标签: c# wcf

实际上我有两个服务,比如服务A和服务B.我试图将服务A称为服务B(就像你说我有一个代理类和调用)。现在的问题是当我从" postman rest客户端打包应用程序访问我的服务A时#34;或"移动"它不起作用。实际上来自客户端("邮递员休息客户端打包应用程序"或"移动")服务A正在调用,但服务A无法调用服务B.(远程服务器返回意外回复:(400)不良请求)。但是,如果我尝试在控制台应用程序中使用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>

1 个答案:

答案 0 :(得分:1)

从根本上说,您将以相同的方式调用Web服务,无论是使用WCF,WebAPI还是其他方式编写的.Net,或者Web服务是在没有.Net的情况下编写的。您必须向URI发出HTTP请求。

这取决于您的来电地点。使用HTTP(S)请求调用所有Web服务,这就是使它们成为Web服务的原因。

使用.Net制作Web请求有几种简单的机制,包括

System.Net.WebClient

和更新的

System.Net.Http.HttpClient

或者,您可以尝试使用WCF client approach expanded here,但我建议现在这已经过时了。 Web服务世界进入了RestFul,WCF和更简单,更高效WebAPI