我不想为每一个数据协定设置命名空间,所以我决定在我的DataContracts所在项目的AssemblyInfo.cs文件中使用ContractNamespace属性。这是条目
[assembly: ContractNamespace("http://www.mycompany.com/MyProject/", ClrNamespace = "Company.Project.Client.Entities")]
这没有任何作用。我似乎必须在所有数据协定上指定命名空间,如下所示:
[DataContract(Namespace = "http://www.mycompany.com/MyProject/")]
..是的,我已经验证我的DataContracts实际上在指定的ClrNamespace中。为什么这不起作用?我正在使用.net 4.5。感谢。
更新:以下是我在合同上使用显式命名空间时生成的WSDL:
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:i0="http://www.company.com/Project/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" name="AccountService" targetNamespace="http://tempuri.org/">
<wsdl:import namespace="http://www.company.com/Project/" location="http://my.server.local/Project/Account/AccountService.svc?wsdl=wsdl0"/>
<wsdl:types/>
<wsdl:binding name="BasicHttpBinding_IAccountService" type="i0:IAccountService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="GetByAccountNumber">
<soap:operation soapAction="http://www.company.com/Project/IAccountService/GetByAccountNumber" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="AccountService">
<wsdl:port name="BasicHttpBinding_IAccountService" binding="tns:BasicHttpBinding_IAccountService">
<soap:address location="http://my.server.local/Project/Account/AccountService.svc"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
这是我在AssemblyInfo.cs中尝试使用ContractNamespace属性时看到的WSDL
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" name="AccountService" targetNamespace="http://tempuri.org/">
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="http://my.server.local/Project/Account/AccountService.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
<xsd:import schemaLocation="http://my.server.local/Project/Account/AccountService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
<xsd:import schemaLocation="http://my.server.local/Project/Account/AccountService.svc?xsd=xsd2" namespace="http://www.company.com/Project/"/>
<xsd:import schemaLocation="http://my.server.local/Project/Account/AccountService.svc?xsd=xsd3" namespace="http://schemas.datacontract.org/2004/07/Company.Project.Core"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="IAccountService_GetByAccountNumber_InputMessage">
<wsdl:part name="parameters" element="tns:GetByAccountNumber"/>
</wsdl:message>
<wsdl:message name="IAccountService_GetByAccountNumber_OutputMessage">
<wsdl:part name="parameters" element="tns:GetByAccountNumberResponse"/>
</wsdl:message>
<wsdl:portType name="IAccountService">
<wsdl:operation name="GetByAccountNumber">
<wsdl:input wsaw:Action="http://tempuri.org/IAccountService/GetByAccountNumber" message="tns:IAccountService_GetByAccountNumber_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IAccountService/GetByAccountNumberResponse" message="tns:IAccountService_GetByAccountNumber_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpBinding_IAccountService" type="tns:IAccountService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="GetByAccountNumber">
<soap:operation soapAction="http://tempuri.org/IAccountService/GetByAccountNumber" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="AccountService">
<wsdl:port name="BasicHttpBinding_IAccountService" binding="tns:BasicHttpBinding_IAccountService">
<soap:address location="http://my.server.local/Project/Account/AccountService.svc"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
答案 0 :(得分:0)
好的,我这里有两个问题。首先是ClrNamespace中的一个拼写错误,我无法相信这让我长时间注意到了。第二个是我的合同在一个项目中,比如说Company.Project.Contracts。该项目有几个文件夹,比方说数据和服务(用于数据和服务合同),并在名称空间Company.Project.Contracts中有一些合同。因此,在我的AssemblyInfo.cs文件中,我必须具有MULTIPLE ContractNamespace属性,如下所示:
[assembly: ContractNamespace("http://www.company.com/Project/", ClrNamespace = "Company.Project.Business.Contracts")]
[assembly: ContractNamespace("http://www.company.com/Project/", ClrNamespace = "Company.Project.Business.Contracts.Service")]
[assembly: ContractNamespace("http://www.company.com/Project/", ClrNamespace = "Company.Project.Business.Contracts.Data")]
另外,@ nodots,感谢指针/帮助。
答案 1 :(得分:0)
ContractNamespace仅适用于数据合同。您仍然需要在[ServiceContract]上设置服务名称空间。
该行
<xsd:import schemaLocation="http://my.server.local/Project/Account/AccountService.svc?xsd=xsd2" namespace="http://www.company.com/Project/"/>
您的WSDL中的表示ContractNamespace属性确实有效。