C# WCF 测试客户端不支持操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9846746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 10:53:04  来源:igfitidea点击:

Operation not supported in the WCF Test Client

c#wcf

提问by Lord of Scripts

I created a WCF service and to the default service I added another operation contract on the main DataContract:

我创建了一个 WCF 服务,并在主 DataContract 上向默认服务添加了另一个操作合同:

[OperationContract]
void DoSomething(UserData data);

Then I have something like this (simplified for the purpose of example) below. The problem is that even though ALL classes in the hierarchy are decorated with DataContract and ALL their members decorated with DataMember, when I use the WCF Test Client it has a red icon indicating that "the operation is not supported in the WCF test client".

然后我在下面有这样的东西(为了示例而简化)。问题是,即使层次结构中的所有类都用 DataContract 修饰,并且它们的所有成员都用 DataMember 修饰,但当我使用 WCF 测试客户端时,它有一个红色图标,指示“WCF 测试客户端不支持该操作”。

[DataContract]
public class UserData {
   [DataMember]
   public uint One { get; set; }

   [DataMember]
   public CompositeType Extra { get; set; }

   public UserData() { ctor. code }
}


[DataContract]
public class CompositeType {
    [DataMember]
    public uint Two { get; set; }

    public UserData() { ctor code }
}

采纳答案by Phill

Add the attribute to your 'UserData' class [KnownType(typeof(CompositeType))]

将属性添加到您的“UserData”类 [KnownType(typeof(CompositeType))]

Like:

喜欢:

[DataContract]
[KnownType(typeof(CompositeType))]
public class UserData 
{
   [DataMember]
   public uint One { get; set; }

   [DataMember]
   public CompositeType Extra { get; set; }

   public UserData() { ctor. code }
}

http://msdn.microsoft.com/en-us/library/ms730167.aspx

http://msdn.microsoft.com/en-us/library/ms730167.aspx

Edit:

编辑:

http://msdn.microsoft.com/en-us/library/system.operatingsystem.aspx

http://msdn.microsoft.com/en-us/library/system.operatingsystem.aspx

The OperatingSystem class has a few properties which relate to other classes. You could include all these classes in the known types but the dependency chain could get rather large and I would highly recommend not using the Operating System class at all.

操作系统类有一些与其他类相关的属性。您可以在已知类型中包含所有这些类,但依赖链可能会变得相当大,我强烈建议根本不使用操作系统类。

You should work out what information you actually need from the Operating System class and create your own DTO to pass back in the response. That way you can ensure all the types are easily definable on your contract.

您应该从操作系统类中找出您实际需要的信息,并创建您自己的 DTO 以在响应中传回。这样您就可以确保所有类型都可以在您的合同中轻松定义。

回答by kjosh

Does the service work if you create a test client(like a console app) and add a service reference to the wcf? If it does, then your datacontract probably has one of those types not supported by WCF Test client.

如果您创建测试客户端(如控制台应用程序)并向 wcf 添加服务引用,该服务是否有效?如果是,那么您的数据契约可能具有 WCF 测试客户端不支持的类型之一。

See this related issue

看到这个相关问题

回答by marc_s

The WCF default expectation for a service call is request-response - WCF expects some kind of a response back.

WCF 对服务调用的默认期望是请求-响应 - WCF 期望得到某种类型的响应。

If you want to use void(as in: no return value), you need to decorate those methods with

如果你想使用void(如:无返回值),你需要用

[OperationContract(IsOneWay = true)]
void DoSomething(UserData data);

to tell the WCF runtime notto expect any return value from the call

告诉 WCF 运行时不要期望从调用中获得任何返回值

Read more about WCF: Working with One-Way Calls, Callbacks and Eventshere in MSDN Magazine.

在 MSDN 杂志中阅读有关WCF:使用单向调用、回调和事件的更多信息。

回答by Lord of Scripts

OK, having gone through the whole thing (thanks to all for the tips) the solution was this:

好的,经历了整个事情(感谢所有人的提示),解决方案是这样的:

  • IsReferenceattribute in DataContractwas not needed at all
  • IsOneWayattribute in DataContractwas not needed at all even when OperationContractwas returning void.
  • KnownTypewas also not needed provided all the subtypes in the hierarchy were mine, in other words defined by me rather than .NET and marked with DataContractor DataMemberas appropriate
  • Getting rid of OperatingSystemand building a wrapper DataContractthat extracted the necesary information from OperatingSystemgot around the issue.
  • IsReferenceDataContract根本不需要属性 in
  • IsOneWayDataContract即使OperationContract返回void ,也根本不需要属性in 。
  • KnownType也不需要层次结构中的所有子类型都是我的,换句话说,由我而不是 .NET 定义并标记为DataContractDataMember适当
  • 摆脱OperatingSystem并构建一个DataContractOperatingSystem解决问题中提取必要信息的包装器。

Now there is no error in the WCF Test Client

现在WCF测试客户端没有错误