2017-03-06 60 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> 
相關問題