在IIS中托管具有Castle Windsor依赖项注入的WCF服务
我第一次尝试创建WCF服务,该服务将托管在带有IIS的服务器上。该服务将依赖于一个单独的类来从SQL数据库检索数据,因此我试图使用Castle Windsor通过IoC保持这种松散耦合 以下是WCF服务的各个方面(提前向任何C#er道歉,但我是VB人!): 服务合同在IIS中托管具有Castle Windsor依赖项注入的WCF服务,wcf,asp.net-mvc-4,iis-7.5,castle-windsor,Wcf,Asp.net Mvc 4,Iis 7.5,Castle Windsor,我第一次尝试创建WCF服务,该服务将托管在带有IIS的服务器上。该服务将依赖于一个单独的类来从SQL数据库检索数据,因此我试图使用Castle Windsor通过IoC保持这种松散耦合 以下是WCF服务的各个方面(提前向任何C#er道歉,但我是VB人!): 服务合同 <ServiceContract()> _ Public Interface IGetRiskDataService <OperationContract()> Function GetRi
<ServiceContract()> _
Public Interface IGetRiskDataService
<OperationContract()>
Function GetRisksByInsuredOpenBoxID(ByVal openBoxID As String) As List(Of Risk)
End Interface
Public Class GetRiskDataService : Implements IGetRiskDataService
Private _rep As IDataWarehouseRepository
Public Sub New(ByVal rep As IDataWarehouseRepository)
_rep = rep
End Sub
Public Function GetRisksByInsuredOpenBoxID(openBoxID As String) As List(Of Risk) Implements IGetRiskDataService.GetRisksByInsuredOpenBoxID
Return _rep.GetRisksByInsuredOpenBoxID(CType(openBoxID, Integer))
End Function
End Class
如您所见,我正在将用于从SQL数据库获取数据的类注入WCF服务的构造函数中
svc文件
<% @ServiceHost Service="DataWarehouseWCFService.GetRiskDataService" Factory="Castle.Facilities.WcfIntegration.DefaultServiceHostFactory, Castle.Facilities.WcfIntegration" %>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service behaviorConfiguration="DataWarehouseWCFService.GetRiskDataServiceBehavior"
name="DataWarehouseWCFService.GetRiskDataService">
<!--<endpoint address="" binding="wsHttpBinding" contract="DataWarehouseWCFService.IGetRiskDataService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>-->
<!--<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />-->
<endpoint address="../GetRiskDataService.svc"
binding="webHttpBinding"
contract="DataWarehouseWCFService.IGetRiskDataService"
behaviorConfiguration="webBehaviour" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/DataWarehouseWCFService/GetRiskDataService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="DataWarehouseWCFService.GetRiskDataServiceBehavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="False"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
因此,这里我向Castle Windsor注册了服务,包括对SQL数据检索类的依赖。希望到目前为止,一切顺利
我已在IIS服务器上部署并安装了此托管应用程序,没有任何问题
所以剩下的就是在客户端应用程序中使用服务,这就是我要解决的问题。我有一个MVC4应用程序,希望在其中使用web服务在视图中显示数据。我已经在此应用程序中使用Castle Windsor来注册其他依赖项,因此所需的所有Castle Windsor基础设施都已存在。因此,我认为我需要的只是一个web服务的安装程序。我的尝试如下:
Public Class DataWarehouseWebServicesInstaller : Implements IWindsorInstaller
Private Const _getRiskDataServiceEndpoint As String = "http://[Server Name].[Domain Name]/Data Warehouse Web Services/GetRiskDataService.svc"
Public Sub Install(container As Castle.Windsor.IWindsorContainer, store As Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore) Implements Castle.MicroKernel.Registration.IWindsorInstaller.Install
'Data Warehouse Web Services
container.Register(Component.For(Of IEndpointBehavior).ImplementedBy(Of WebHttpBehavior))
container.AddFacility(Of WcfFacility).Register( _
Component.For(Of IGetRiskDataService).AsWcfClient( _
New DefaultClientModel( _
WcfEndpoint.BoundTo(New WebHttpBinding).At(_getRiskDataServiceEndpoint) _
) _
) _
)
End Sub
End Class
最后,我向控制器添加了一些测试代码,以查看是否可以让WCF服务工作:
Public Sub New(ByVal ash As IGetRiskDataService)
Dim lozza = ash.GetRisksByInsuredOpenBoxID(100005859)
For Each lianne As Risk In lozza
Dim neil As Date = lianne.ExpiryDate.CalendarDate
Next
End Sub
对此进行测试时,我得到以下错误:
{"The remote server returned an error: (415) Cannot process the message because the content type 'application/xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.."}. The client and service bindings may be mismatched.
这就是我迷路的地方。我已经研究了客户端绑定和服务绑定不匹配的问题,但不幸的是,几乎每个答案都提到了app.config文件中指定的这些绑定,我没有这样做,因为我依赖Castle Windsor来处理大部分问题,我所做的任何配置我都想在代码中完成。例如,在我的安装程序代码中,我用WebHttpBinding注册了一个端点
我觉得我很接近这一点,但已经撞到了众所周知的砖墙。任何帮助都将不胜感激。请让我知道,如果有任何其他信息,我可以提供给你
非常感谢,
Ash当您的服务中存在错误配置时,该错误是一个相当普遍的错误。该服务根本没有正常运行,并返回一个标准的wcf html错误页面-客户端希望得到soap响应,但它得到的只是错误html 要找出到底是什么地方出了问题,您应该在浏览器中导航到您的服务
http://localhost:8732/Design_Time_Addresses/DataWarehouseWCFService/GetRiskDataService/GetRiskDataService.svc
此处显示的错误消息将更具描述性
只要看看你目前发布的内容,它可能与
- 您的风险类别是否标记为具有DataMember属性的DataContract
- 您可能不需要端点地址中的address=“../GetRiskDataService.svc”部分
http://localhost:8732/Design_Time_Addresses/DataWarehouseWCFService/GetRiskDataService/GetRiskDataService.svc