是否可以通过城堡windsor注入WCF REST服务客户端

时间:2017-03-06 10:40:15

标签: .net asp.net-mvc web-services wcf castle-windsor

我创建了两个托管在不同端口的WCF休息服务service1service2,并在Castle Windsor中使用service1进行依赖注入,现在从service1尝试致电service2。但是当我创建一个实例并调用service2时,我得到一个例外bad request(400)。当我从REST客户端请求service2时,我能够获得200 response。错误是由于温莎城堡生活方式的行为吗?。

容器配置(服务1)

Container.AddFacility<WcfFacility>().Register(
                           Component.For(type).AsWcfClient(
                               new DefaultClientModel { 
                                       Endpoint = WcfEndpoint.FromConfiguration("*") }));

配置(服务1)

<system.serviceModel>
<client>
    <endpoint address="http://localhost:8082/BLDBService" binding="webHttpBinding" bindingConfiguration="customHttpBinding" contract ="DataSourceContracts.IBLDBService" behaviorConfiguration ="serviceEndpointHttpBehavior">
  </endpoint>
</client>
<services>
  <service name="BusinessService.MetaDataService" behaviorConfiguration="basicHttpBehavior">        
    <host>
      <baseAddresses >
        <add baseAddress ="http://localhost:8084/"/>
      </baseAddresses>
    </host>
   <endpoint address="MetaDataService" binding="webHttpBinding" contract ="BusinessServiceContracts.IMetaDataService" behaviorConfiguration="endpointHttpBehavior" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<bindings>
  <!--<basicHttpsBinding>
    <binding name="BasicHttpsBinding">
      <security mode="Transport">
        <transport clientCredentialType="None"></transport>
      </security>
    </binding>
  </basicHttpsBinding>-->
  <webHttpBinding>
    <binding name ="customHttpBinding" transferMode ="Streamed">          
    </binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="endpointHttpBehavior">
      <webHttp helpEnabled ="true" automaticFormatSelectionEnabled ="true" defaultOutgoingResponseFormat ="Json"></webHttp>
    </behavior>
    <behavior name="serviceEndpointHttpBehavior">
      <webHttp defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled ="true" helpEnabled ="true"  />
      <dataContractSerializer/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="basicHttpBehavior">
     <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

服务电话(服务1)

_bldbService = Container.resolve<IBLDBService>();
WebOperationContext.Current.OutgoingRequest.ContentType = "application/json";
viewModel = _bldbService.GetBLDBData(viewModel);

配置(服务2)

 <service name="BusinessService.BLDBService" behaviorConfiguration="basicHttpBehavior">
    <!--<endpoint address="net.tcp://localhost:8082/BLDBService" binding="netTcpBinding" contract="BusinessServiceContracts.IBLDBService"/>-->
    <host>
      <baseAddresses>
        <add baseAddress ="http://localhost:8082/"/>
      </baseAddresses>
    </host>
    <endpoint address="BLDBService" binding="webHttpBinding"    contract ="BusinessServiceContracts.IBLDBService" behaviorConfiguration="endpointHttpBehavior"/>
  </service>

从REST客户端调用

enter image description here

P.S。我已经使用net / tcp协议进行了测试,它运行良好。

enter image description here

这是日志

        Time : 06.03.2017 11:11:15
    ----------------------------------------------------------------------------------------------------------------
    Message: The remote server returned an unexpected response: (400) Bad Request.
    ----------------------------------------------------------------------------------------------------------------
    Environment: Castle.Facilities.WcfIntegration
    ----------------------------------------------------------------------------------------------------------------
    Stack Trace: at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory`1 factory, WebException responseException, ChannelBinding channelBinding)
       at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
       at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
       at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
       at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
       at Castle.Facilities.WcfIntegration.Proxy.WcfRemotingInterceptor.InvokeRealProxy(RealProxy realProxy, WcfInvocation wcfInvocation)
       at Castle.Facilities.WcfIntegration.WcfInvocation.Proceed()
       at Castle.Facilities.WcfIntegration.RepairChannelPolicy.Apply(WcfInvocation wcfInvocation)
       at Castle.Facilities.WcfIntegration.Proxy.WcfRemotingInterceptor.PerformInvocation(IInvocation invocation, Action`1 action)
       at Castle.DynamicProxy.AbstractInvocation.Proceed()
       at Castle.Proxies.IBLDBServiceProxy.GetBLDBData(MetaDataViewModel viewModel)
       at BusinessLogic.BLDBBusinessManager.GetData(MetaDataViewModel viewModel) in C:\localPTC\Sample\BasicFramework\BusinessLogic\BLDB\BLDBBusinessManager.cs:line 46
       at BusinessService.MetaDataService.GetMetaData(MetaDataViewModel metadata) in C:\localPTC\Sample\BasicFramework\BusinessService\Services\MetaDataService.cs:line 16
    ----------------------------------------------------------------------------------------------------------------


    Time : 06.03.2017 11:11:15
    ----------------------------------------------------------------------------------------------------------------
    Message: The remote server returned an error: (400) Bad Request.
    ----------------------------------------------------------------------------------------------------------------
    Environment: System
    ----------------------------------------------------------------------------------------------------------------
    Stack Trace: at System.Net.HttpWebRequest.GetResponse()
       at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
    ----------------------------------------------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:0)

终于找到了答案。

我将服务的请求 - 响应格式硬编码为JSON。现在我把它从服务合同中删除了,它的工作完美无缺。新的服务合同和web.config如下所示。

  <behaviors>
      <endpointBehaviors>
        <behavior name="endpointHttpBehavior">
          <webHttp helpEnabled="true" automaticFormatSelectionEnabled="true"  defaultBodyStyle="Bare"></webHttp>
        </behavior>
    </endpointBehaviors>
</behaviors>

的Web.Config

{{1}}