添加到共享类的多个WCF服务的服务引用 [英] Adding service references to multiple WCF services that shared classes

查看:79
本文介绍了添加到共享类的多个WCF服务的服务引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图我的WCF Web服务分成几个服务,而不是1巨服务。但是,在Visual Studio(Silverlight客户端)通过复制两个服务共享的公共类。下面是一个简单的例子来说明我的问题。

I'm attempting to split up my WCF web services into a few services instead of 1 giant service. But the Visual Studio (Silverlight client) duplicates the common classes shared by both services. Here is a simple example to illustrate my problem.

在这个例子中有两个服务。两种方法都返回类型为人。默认情况下VS将创建两个单独人代理的下唯一的命名空间。这意味着,人,由不同的服务返回的不能由客户端为相同的东西被消耗。我该如何解决这个问题?是否有可能无需编写代理类自己?

In this example there are two services. Both return the type "Person". By default VS will create two seperate Person proxy's under unique NameSpaces. This means that the "Person" returned by the different services cannot be consumed by the client as the same thing. How do I fix this? Is it possible without writing the proxy classes myself?

[DataContract]
public class Person
{
    [DataMember]
    string FirstName { get; set; }
    [DataMember]
    string LastName { get; set; }
    [DataMember]
    string PrivateData { get; set; }
}



StaffService.svc



StaffService.svc

[ServiceContract(Namespace = "")]
public class StaffService
{
     [OperationContract]
     public Person GetPerson ()
     {
         return new Person {"John", "Doe", "secret"};
     };
}



PublicService.svc



PublicService.svc

[ServiceContract(Namespace = "")]
public class PublicService
{
     [OperationContract]
     public Person GetPerson ()
     {
         return new Person {"John", "Doe", "*****"};
     };
}



感谢您的帮助!
贾斯汀

Thanks for you help! Justin

推荐答案

有处于添加服务引用评选的重用类型的高级部分中的复选框引用的程序集。这将追捕在服务中使用的类型,如果他们在一个引用的程序集已经存在那么他们就会被使用,而不是生成的代理类。

There is a check box under the Advanced section of "Add Service Reference" named "Reuse types in referenced assemblies". This will hunt for types used in your service and if they already exist in a referenced assembly then they'll be used rather than a proxy class generated.

一个需要注意的是这里它只是被搜索,这样就不会拿起其他服务生成的代理(我相信不同的命名空间将停止它)引用的程序集。

One caveat here is that it's only "referenced assemblies" that are searched, so it won't pick up proxies generated by other services (and I believe the different namespace would stop it as well).

我通常有一个业务/领域的项目在我的Silverlight项目,所以我加我的共享类到该项目(通常使用添加现有项 - >添加为链接这样的代码共享)。

I usually have a business / domain project in my Silverlight project so I add my shared classes to that project (usually with the "Add Existing Item" > "Add as Link" so the code is shared).

一旦这样做,你可以生成您服务引用和他们应该拿起您现有的类型。

Once that's done you can generate your service references and they should pick up your existing types.

希望这帮助

这篇关于添加到共享类的多个WCF服务的服务引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆