WCF单服务多合同和泛型,可能吗?
我可以拥有一个具有多个端点和多个使用泛型的契约的服务吗。我遇到了一个无法创建元数据的问题(它暗示这可能只是一个配置问题,不确定我的基本主机地址应该是什么样子):WCF单服务多合同和泛型,可能吗?,wcf,web-services,generics,Wcf,Web Services,Generics,我可以拥有一个具有多个端点和多个使用泛型的契约的服务吗。我遇到了一个无法创建元数据的问题(它暗示这可能只是一个配置问题,不确定我的基本主机地址应该是什么样子): 名称空间WCFSingleService { //注意:您可以使用“重构”菜单上的“重命名”命令同时更改代码和配置文件中的接口名称“IService1”。 [服务合同] 公共接口服务 { [经营合同] T获取数据(T项); } } 命名空间WCFSingleService { [服务合同(Name=“User”)] 公共接口IUs
名称空间WCFSingleService
{
//注意:您可以使用“重构”菜单上的“重命名”命令同时更改代码和配置文件中的接口名称“IService1”。
[服务合同]
公共接口服务
{
[经营合同]
T获取数据(T项);
}
}
命名空间WCFSingleService
{
[服务合同(Name=“User”)]
公共接口IUserSingleService:ISingleService
{
}
}
命名空间WCFSingleService
{
[ServiceContract(Name=“Some”)]
公共接口服务:ISingleService
{
}
}
公共部分类SingleService:IUserSingleService
{
公共用户GetData(用户项)
{
//做点什么
}
}
公共部分类SingleService:SingleService
{
公共某些GetData(某些项)
{
//做点什么
}
}
这可能吗?此服务的配置是什么样的?另外,我是否能够从(比如)AJAX客户机使用该服务?我想我会的,因为我不想把一个类型传递给合同,每个合同都有自己的端点,对吗?谢谢
以下是我当前的配置:
<system.serviceModel>
<services>
<service name="WCFSingleService.SingleService" behaviorConfiguration="WCFSingleService.ServiceBehavior">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8732/Design_Time_Addresses/WCFSingleService/SingleService" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="User" binding="wsHttpBinding" contract="WCFSingleService.IUserSingleService"/>
<endpoint address="Some" binding="wsHttpBinding" contract="WCFSingleService.ISomeSingleService"/>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFSingleService.ServiceBehavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- 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="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
更新:
嗯,我想弄明白为什么我的服务不好;t wroking,一旦我打开调试,就会出现错误dorr。哼!无论如何,我遇到的问题与为两个服务创建相同的方法名有关。那么,如果多个服务推动同一个接口,有人知道让WCF重命名方法名的方法吗?我是否可以在其中一个实现中对方法进行修饰,使其看起来与众不同?我必须这样做 我试图让服务返回接口,因为它可以返回多种类型。最后,它的反应就像返回对象一样(糟糕) 相反,我为每个可能返回的类型实现了不同的回调函数:
[ServiceContract(CallbackContract=typeof(IClientFeedback))]
public interface IDataService
{
[OperationContract]
void GetItem(string entryID);
}
[ServiceContract]
public interface IClientFeedback
{
[OperationContract(IsOneWay = true)]
void ReturnMailMessage(MailMessage msg);
[OperationContract(IsOneWay = true)]
void ReturnContact(Contact cont);
}
我知道这不太一样,但如果接口不起作用,我认为泛型更不可能出现。是的,您可以使用多个合同提供单个服务,您必须在服务接口上设置ConfigurationName 您需要像这样声明您的接口
Namespace ServiceNameSpace
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0"), _
System.ServiceModel.ServiceContractAttribute([Namespace]:="whatever namespace you like", ConfigurationName:="ServiceContract1")> _
Public Interface ServiceContract1
<System.ServiceModel.OperationContractAttribute(Action:="Service Action"), _
System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults:=True)> _
Function ServiceFunction1(ByVal var As Class1) As Class1
End Interface
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0"), _
System.ServiceModel.ServiceContractAttribute([Namespace]:="temp namespace", ConfigurationName:="ServiceContract2")> _
Public Interface ServiceContract2
<System.ServiceModel.OperationContractAttribute(Action:="Service Action"), _
System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults:=True)> _
Function function2(ByVal var As Class2) As Class2
End INterface
End Namespace
最后,只需配置如下所示的服务
<system.serviceModel>
<services>
<service name="ServiceNameSpace.ServiceImplementationCLass" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="basicHttpBinding" contract="ServiceContract1" />
<endpoint address="" binding="basicHttpBinding" contract="ServiceContract2" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- 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="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
上面的代码在VB.NET中,如果您需要,我也可以为您提供C#代码
我希望我的解决方案能帮到你。我已经大致明白了我想要实现的目标。基本上,我仍然可以在拥有一个服务的同时将代码设置为使用泛型。我想到了单一服务。然后我意识到我需要在部分类SingleService(而不是接口本身)上指定ServiceContract,并用OperationContract(Name=“TheExposedNameOfMethod”)修饰我的实现方法。下面是一些代码:
public interface ISingleService<T>
{
//[OperationContract]
T GetData(T item);
}
public interface IUserSingleService: ISingleService<User>
{
}
public interface IOtherSingleService: ISingleService<Other>
{
}
[ServiceContract]
public partial class SingleService : IUserSingleService
{
[OperationContract(Name = "GetDataUser")]
public User GetData(User item)
{
switch(item.MessageCommand)
{
case "Create":
//do stuff to for a User create
break;
case "Update":
//do stuff to for a User update
break;
case "Delete":
//do stuff to for a User Delete
break;
}
return item;
}
}
//You only need to specifc the ServiceContract attribute in one of the partial classes
public partial class SingleService : IOtherSingleService
{
[OperationContract(Name = "GetDataOther")]
public Other GetData(Other item)
{
...do something
return item;
}
}
公共接口是单一服务
{
//[经营合同]
T获取数据(T项);
}
公共接口IUserSingleService:ISingleService
{
}
公共接口IOtherSingleService:ISingleService
{
}
[服务合同]
公共部分类SingleService:IUserSingleService
{
[操作合同(Name=“GetDataUser”)]
公共用户GetData(用户项)
{
开关(item.MessageCommand)
{
案例“创建”:
//为用户创建一些东西
打破
案例“更新”:
//为用户更新做一些事情
打破
案例“删除”:
//对用户执行删除操作
打破
}
退货项目;
}
}
//您只需要在其中一个分部类中指定ServiceContract属性
公共部分类单服务:IOtherSingleService
{
[运营合同(Name=“GetDataOther”)]
公共其他GetData(其他项)
{
…做点什么
退货项目;
}
}
以下是端点的外观:
namespace WCFSingleService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface ISingleService<T>
{
[OperationContract]
T GetData(T item);
}
}
namespace WCFSingleService
{
[ServiceContract(Name = "User")]
public interface IUserSingleService: ISingleService<User>
{
}
}
namespace WCFSingleService
{
[ServiceContract(Name = "Some")]
public interface ISomeSingleService: ISingleService<Some>
{
}
}
public partial class SingleService : IUserSingleService
{
public User GetData(User item)
{
//Do something
}
}
public partial class SingleService : ISomeSingleService
{
public Some GetData(Some item)
{
//Do something
}
}
<endpoint binding="basicHttpBinding" name="TheService"
contract="SingleService" />
我可以想到使用一个端点和一个服务,但不能使用一个具有多个契约的服务。嗯……我不这么认为,据我所知,WCF不允许泛型。我想我可能把人们和“泛型”这个词混淆了。如果您看看我上面的实现,我公开的实际服务已经是特定类型的了。我不是要求客户把类型传给我。另外,请查看我对原始问题的更新。不过,谢谢你的回复。这确实有帮助。
<endpoint binding="basicHttpBinding" name="TheService"
contract="SingleService" />