2017-03-06 61 views
2

我已经创建了两个WCF rest服务service1和service2托管在不同的端口中,并使用Castle Windsor在service1中依赖注入,现在从service1尝试调用service2。但是当我创建一个实例并调用service2时,我收到一个异常说不好的请求(400)。当我从REST客户端请求service2时,我能够获得200响应。温莎城堡生活方式的行为是错误吗?下面是(服务1)是否有可能通过城堡windsor注入WCF REST服务客户端

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)

用于调用服务2.

容器配置的代码

_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

附:我已经用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) 
    ---------------------------------------------------------------------------------------------------------------- 
+0

您共享的代码调用 “服务2”,从 “服务1” 的?你有没有配置“Service2”的设置和容器?你也可以分享这个配置吗?您是否尝试调试并检查它是否达到了“Service2”代码? –

+0

@ chetan-ranpariya如果我从REST客户端调用它将达到并给出200响应,但是当我通过Castle windsor容器实例调用其说的不正确的请求时,这意味着请求去服务器但请求类型无效。 –

+0

当您收到来自服务的“错误请求”响应时,您是否收到任何其他信息,例如错误信息,数据验证错误,授权失败等?当你的调试“Service1”在“Service1”中找到'viewModel = _bldbService.GetBLDBData(viewModel)'行时? –

回答

0

终于找到了答案。

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

namespace DataSourceContracts 
{ 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using ViewModel.DataSource; 

    /// <summary> 
    /// This is same as the exposed BLDB data REST service in abother project we need to keep both in sync. 
    /// The reason for not refering to the same project is the .Net framework version 
    /// </summary> 
    [ServiceContract] 
    public interface IBLDBService : IDataSourceService 
    { 
     /// <summary> 
     /// Sample method 
     /// </summary> 
     /// <param name="viewModel"></param> 
     /// <returns></returns> 
     [OperationContract] 
     [WebInvoke(Method = "POST", UriTemplate = "GetBLDBData")] 
     MetaDataViewModel GetBLDBData(MetaDataViewModel viewModel); 
    } 
} 

的Web.Config

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