繁体   English   中英

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

[英]How to access one WCF service from another?

实际上,我有两个服务,例如Service A和ServiceB。我试图将Service A调用到Service B(就像您说的那样,我有一个代理类并在调用)。 现在的问题是,当我从“邮递员剩余客户端打包的应用程序”或“移动设备”访问我的服务A时,它不起作用。 实际上是从客户端(“邮递员剩余客户端打包的应用程序”或“移动”),服务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>

从根本上讲,无论是以WCF,WebAPI或其他方式在.Net中编写Web服务,还是以没有.Net的方式编写Web服务,您都将以相同的方式调用Web服务。 您必须对URI发出HTTP请求。

这取决于您从哪里打电话。 所有Web服务都通过HTTP(S)请求进行调用,这就是使它们成为Web服务的原因。

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

System.Net.WebClient

和更新的

System.Net.Http.HttpClient

或者,您可以尝试使用此处扩展WCF客户端方法 ,但是我建议这已经过时了。 对于WCF和更简单,更高效的WebAPI来说,Web服务世界都进入了RestFul

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM