简体   繁体   English

返回自定义类的WCF服务在Reference.cs中生成错误

[英]WCF Service that returns a custom class generates errors in Reference.cs

I have a WCF Service project in Visual Studio 2008 that contains about 12 methods, some of which return primitive types like bool or string. 我在Visual Studio 2008中有一个WCF服务项目,它包含大约12个方法,其中一些返回原始类型,如bool或string。 I also have a Visual Studio Unit Test Project that references the published WCF Service. 我还有一个Visual Studio单元测试项目,它引用了已发布的WCF服务。 The Test Project compiles successfully when all the return types are primitive. 当所有返回类型都是原始时,Test Project会成功编译。

If I add a new method to the service that returns a custom class, publish it and update the service reference in the Test Project, it doesn't compile. 如果我向返回自定义类的服务添加一个新方法,发布它并更新测试项目中的服务引用,它就不会编译。 The errors are: - 错误是: -

  1. The type 'PublisherFaultException' already contains a definition for 'Reason'. “PublisherFaultException”类型已包含“Reason”的定义。
  2. The type 'PublisherFaultException' already contains a definition for 'PropertyChanged'. “PublisherFaultException”类型已包含“PropertyChanged”的定义。
  3. Type 'Publisher.Test.LibraryReference.PublisherFaultException' already defines a member called 'RaisePropertyChanged' with the same parameter types. 类型'Publisher.Test.LibraryReference.PublisherFaultException'已经定义了一个名为'RaisePropertyChanged'的成员,它具有相同的参数类型。

all in the auto-generated reference.cs file. 全部在自动生成的reference.cs文件中。

The contract for the method of the WCF Service is: - WCF服务方法的合同是: -

Page GetItem(string path);

and the Page class has the DataContract attribute and it's public properties have the DataMember attribute. Page类具有DataContract属性,它的公共属性具有DataMember属性。

I'm reluctant to modify the Reference.cs file as I'll need to do this every time the Service is updated. 我不愿意修改Reference.cs文件,因为每次更新服务时我都需要这样做。

Anyone know why this is happening? 任何人都知道为什么会这样吗?

Stuart. 斯图尔特。

When you Add Service Reference, you get a 'reuse types in assembly' option - this is likely to be the key to sorting out the duplication. 当您添加服务引用时,您将获得“在程序集中重用类型”选项 - 这可能是整理复制的关键。

Or do you have some Test References that are causing the duplication? 或者你有一些引起重复的测试参考?

Also, have a look in the References section of the project tree and see if there is anything unexpected in there (do you have references to 2 assemblies that both contain Service References in the same namespace?). 另外,看一下项目树的References部分,看看那里是否有任何意外的内容(你是否引用了两个程序集,这两个程序集在同一名称空间中都包含服务引用?)。

Using auto-generated proxy class it is always pain. 使用自动生成的代理类总是很痛苦。

To handle situation like this I using separate assembly with data contract classes and service interface. 为了处理这样的情况,我使用单独的程序集与数据契约类和服务接口。

Contract dll will have: 合同dll将具有:


public interface IService
{
    [OperationContract]
    List GetContentList();
}

[DataContract]
public class ContentItem
{
  [DataMember] public string Name;
  [DataMember] public object Data;
}

The client will have reference to the Contract.dll. 客户端将引用Contract.dll。 Proxy will be created manually: 代理将手动创建:


class ServiceProxy : ClientBase<IService>, IService
 {
  public List GetContentList()
  {
   return Channel.GetContentList();
  }
 }

The server dll will have reference to the same Contract dll. 服务器dll将引用相同的Contract dll。 So we will be able to avoid any errors with auto generated proxy. 因此,我们将能够避免自动生成代理的任何错误。 Also the manually created proxy will be simpler, more manageable. 此外,手动创建的代理将更简单,更易于管理。

When adding the Service Reference, try clicking Advanced, and select "Generate asynchronous operations". 添加服务引用时,请尝试单击“高级”,然后选择“生成异步操作”。

I think what was happening was that there were some asynchronous methods in the web service, with names ending in "Async", which would conflict with the methods generated in the References.cs. 我认为发生的事情是Web服务中存在一些异步方法,名称以“Async”结尾,这将与References.cs中生成的方法冲突。

eg imagine the web service contains 2 methods: (1) SayHello and (2) SayHelloAsync . 例如,假设Web服务包含两种方法:(1) SayHello和(2) SayHelloAsync

Generating using the default task-based method produces: 使用默认的基于任务的方法生成会产生:

  • SayHello and SayHelloAsync for (1) SayHelloSayHelloAsync (1)
  • SayHelloAsync and SayHelloAsyncAsync for (2). (2)的SayHelloAsyncSayHelloAsyncAsync

The conflict occurred because there were 2 generated methods called SayHelloAsync . 发生冲突是因为有两个生成的方法叫做SayHelloAsync

At least I think that's what was going on. 至少我认为这是发生了什么。 Anyway setting "Generate asynchronous operations" worked for me. 无论如何设置“生成异步操作”对我有用。

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

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