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

发布于 2024-08-09 11:14:30 字数 704 浏览 1 评论 0原文

我在 Visual Studio 2008 中有一个 WCF 服务项目,其中包含大约 12 个方法,其中一些方法返回基本类型,如 bool 或 string。我还有一个引用已发布的 WCF 服务的 Visual Studio 单元测试项目。当所有返回类型均为原始类型时,测试项目编译成功。

如果我向返回自定义类的服务添加一个新方法,发布它并更新测试项目中的服务引用,它不会编译。错误是: -

  1. 类型“PublisherFaultException”已包含“Reason”的定义。
  2. 类型“PublisherFaultException”已包含“PropertyChanged”的定义。
  3. 类型“Publisher.Test.LibraryReference.PublisherFaultException”已经定义了一个名为“RaisePropertyChanged”的成员,具有相同的参数类型。

全部在自动生成的reference.cs文件中。

WCF 服务方法的契约是: -

Page GetItem(string path);

Page 类具有 DataContract 属性,其公共属性具有 DataMember 属性。

我不愿意修改 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. I also have a Visual Studio Unit Test Project that references the published WCF Service. The Test Project compiles successfully when all the return types are primitive.

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'.
  2. The type 'PublisherFaultException' already contains a definition for 'PropertyChanged'.
  3. Type 'Publisher.Test.LibraryReference.PublisherFaultException' already defines a member called 'RaisePropertyChanged' with the same parameter types.

all in the auto-generated reference.cs file.

The contract for the method of the WCF Service is: -

Page GetItem(string path);

and the Page class has the DataContract attribute and it's public properties have the DataMember attribute.

I'm reluctant to modify the Reference.cs file as I'll need to do this every time the Service is updated.

Anyone know why this is happening?

Stuart.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

懒猫 2024-08-16 11:14:30

添加服务引用时,您会获得“程序集中重用类型”选项 - 这可能是解决重复问题的关键。

或者您是否有一些导致重复的测试参考?

另外,请查看项目树的“引用”部分,看看其中是否有任何意外内容(是否有对 2 个程序集的引用,这两个程序集都在同一命名空间中包含服务引用?)。

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?).

灼疼热情 2024-08-16 11:14:30

使用自动生成的代理类总是很痛苦。

为了处理这样的情况,我使用带有数据协定类和服务接口的单独程序集。

Contract dll 将具有:


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

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

客户端将引用 Contract.dll。
代理将手动创建:


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

服务器 dll 将引用相同的 Contract dll。
因此,我们将能够避免自动生成的代理出现任何错误。
此外,手动创建的代理将更简单、更易于管理。

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:


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.
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.
So we will be able to avoid any errors with auto generated proxy.
Also the manually created proxy will be simpler, more manageable.

神仙妹妹 2024-08-16 11:14:30

添加服务引用时,尝试单击“高级”,然后选择“生成异步操作”。

我认为发生的情况是 Web 服务中有一些异步方法,其名称以“Async”结尾,这会与 References.cs 中生成的方法发生冲突。

例如,假设 Web 服务包含 2 个方法:(1)SayHello 和 (2)SayHelloAsync

使用默认的基于任务的方法生成会生成:

  • SayHelloSayHelloAsync for (1)
  • SayHelloAsyncSayHelloAsyncAsync for ( 2)。

发生冲突是因为有 2 个名为 SayHelloAsync 的生成方法。

至少我认为这就是正在发生的事情。无论如何,设置“生成异步操作”对我有用。

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.

e.g. imagine the web service contains 2 methods: (1)SayHello and (2)SayHelloAsync.

Generating using the default task-based method produces:

  • SayHello and SayHelloAsync for (1)
  • SayHelloAsync and SayHelloAsyncAsync for (2).

The conflict occurred because there were 2 generated methods called SayHelloAsync.

At least I think that's what was going on. Anyway setting "Generate asynchronous operations" worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文