简体   繁体   English

将服务引用添加到共享类的多个WCF服务

[英]Adding service references to multiple WCF services that shared classes

I'm attempting to split up my WCF web services into a few services instead of 1 giant service. 我正在尝试将我的WCF Web服务拆分为一些服务,而不是一个巨大的服务。 But the Visual Studio (Silverlight client) duplicates the common classes shared by both services. 但Visual Studio(Silverlight客户端)复制了两个服务共享的公共类。 Here is a simple example to illustrate my problem. 这是一个简单的例子来说明我的问题。

In this example there are two services. 在此示例中,有两个服务。 Both return the type "Person". 两者都返回“Person”类型。 By default VS will create two seperate Person proxy's under unique NameSpaces. 默认情况下,VS将在唯一的NameSpaces下创建两个单独的Person代理。 This means that the "Person" returned by the different services cannot be consumed by the client as the same thing. 这意味着不同服务返回的“Person”不能被客户端消费为同一个东西。 How do I fix this? 我该如何解决? Is it possible without writing the proxy classes myself? 没有自己编写代理类是否可能?

Common 共同

[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). 这里需要注意的是,它只是被搜索的“引用程序集”,因此它不会获取其他服务生成的代理(我相信不同的命名空间也会阻止它)。

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). 我通常在我的Silverlight项目中有一个业务/域项目,所以我将我的共享类添加到该项目中(通常使用“添加现有项”>“添加为链接”,以便共享代码)。

Once that's done you can generate your service references and they should pick up your existing types. 完成后,您可以生成服务引用,他们应该选择现有类型。

Hope this helps 希望这可以帮助

If you generate the proxies at the same time using svcutil.exe it will only generate one type. 如果使用svcutil.exe同时生成代理,则只生成一种类型。 I don't know how to do this with adding a service reference to the project. 我不知道如何通过向项目添加服务引用来实现此目的。

We run it in a batch file so I have clipped that down and changed the names to protect the innocent. 我们在一个批处理文件中运行它,所以我将其剪切并更改名称以保护无辜者。 It is really about mapping the service namespaces together and then including all the URLs together . 它实际上是将服务命名空间映射到一起,然后将所有URL包括在一起 It also has the collection type set (for lists) and includes an assembly reference (which some of the other answers reference. 它还具有集合类型集(用于列表)并包含程序集引用(其他一些答案引用。

@ECHO OFF

SET cmd=C:\"Program Files"\"Microsoft SDKs"\Windows\v6.0a\bin\SvcUtil.exe
SET cmd=%cmd% /out:Traffic.cs /noConfig /collectionType:System.Collections.Generic.List`1

SET cmd=%cmd% /reference:..\..\..\lib\Architecture.Frameworks.dll

REM ######### Service namespace mappings (Service Contracts and Message Contracts)
SET cmd=%cmd% /namespace:"http://services.test.com/app/2005/09/"
SET cmd=%cmd%,"app.ServiceProxies"

REM ######### Schema namespace mappings (Data Contracts)
SET cmd=%cmd% /namespace:"http://schemas.company.com/app/2005/09/"
SET cmd=%cmd%,"Co.ServiceProxies.app.DataContracts"

REM ######### Set all the URLs that have common types
SET cmd=%cmd% http://localhost/Services/MyService1.svc
SET cmd=%cmd% http://localhost/Services/MyService2.svc

%cmd%

PAUSE

If all the items are in the same service namespace, you could possibly get away with just having all the URLs and not worry about the namespaces, but I have not tried it that way. 如果所有项目都在同一个服务命​​名空间中,那么您可能只需拥有所有URL而不用担心命名空间,但我没有尝试过这种方式。

svcutil.exe can reuse types from specified assemblies. svcutil.exe可以重用指定程序集中的类型。 You can configure this by setting service reference properties or by /reference key if you are running svcutil.exe manually. 如果手动运行svcutil.exe,可以通过设置服务引用属性或/ reference键来配置它。

If you are the owner of mentioned services, consider putting data types like Person class into separate assembly which is available from both client and service. 如果您是所提及服务的所有者,请考虑将Person类等数据类型放入单独的程序集中,该程序集可从客户端和服务中获得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM