.net 服务引用未生成客户端类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3000209/
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
Service reference not generating client types
提问by Cranialsurge
I am trying to consume a WCF service in a class library by adding a service reference to it. In one of the class libraries it gets consumed properly and I can access the client types in order to generate a proxy off of them. However in my second class library (or even in a console test app), when i add the same service reference, it only exposes the types that are involved in the contract operations and not the client type for me to generate a proxy against.
我试图通过向它添加服务引用来使用类库中的 WCF 服务。在其中一个类库中,它被正确使用,我可以访问客户端类型以从中生成代理。但是,在我的第二个类库中(甚至在控制台测试应用程序中),当我添加相同的服务引用时,它只公开合同操作中涉及的类型,而不公开客户端类型以供我生成代理。
e.g. Endpoint has 2 services exposed - ISvc1 and ISvc2. When I add a service reference to this endpoint in the first class library I get ISvc1Client andf ISvc2Client to generate proxies off of in order to use the operations exposed via those 2 contracts. In addition to these clients the service reference also exposes the types involved in the operations like (type 1, type 2 etc.) this is what I need. However when i try to add a service reference to the same endpoing in another console application or class library only Type 1, Type 2 etc. are exposed and not ISvc1Client and ISvc2Client because of which I cannot generate a proxy to access the operations I need. I am unable to determine why the service reference gets properly generated in one class library but not in the other or the test console app.
例如,端点公开了 2 个服务 - ISvc1 和 ISvc2。当我在第一个类库中添加对此端点的服务引用时,我让 ISvc1Client 和 f ISvc2Client 生成代理,以便使用通过这两个合同公开的操作。除了这些客户端之外,服务引用还公开了操作中涉及的类型,例如(类型 1、类型 2 等),这正是我需要的。但是,当我尝试在另一个控制台应用程序或类库中添加对相同端点的服务引用时,仅公开了类型 1、类型 2 等,而不是 ISvc1Client 和 ISvc2Client,因此我无法生成代理来访问我需要的操作。我无法确定为什么在一个类库中正确生成了服务引用,而在另一个类库或测试控制台应用程序中却没有。
采纳答案by Cranialsurge
Apparently you have to add a reference to System.Web in your project before adding the Service Reference. That did it.
显然,在添加服务引用之前,您必须在项目中添加对 System.Web 的引用。做到了。
回答by Simon_Weaver
You may have selected Reuse types in specified reference assembliesbut not chosen the very important mscorliblibrary.
您可能选择了Reuse types in specified reference assemblies但没有选择非常重要的mscorlib库。
First click 'Show All Files' at the top of your Solution Explorer so you can expand out the service reference.
首先单击解决方案资源管理器顶部的“显示所有文件”,以便展开服务引用。


- Find the
Reference.csfile and open it. - Search for
ClientBasein the source code to make sure you really haven't generated a client with a name you weren't expecting. If you find it then that's the name of your service client.
- 找到
Reference.cs文件并打开它。 ClientBase在源代码中搜索以确保您确实没有生成具有您不期望的名称的客户端。如果您找到它,那么这就是您的服务客户端的名称。


If nothing matches then right click the service reference and choose Configure Service Reference.
如果没有匹配项,则右键单击服务引用并选择Configure Service Reference。
The important one is mscorlibwhich is required to properly generate the client. I like to select System.Xml.Linqalso to get nice Linq classes like XElementand not XmlElement.
重要的mscorlib是需要正确生成客户端。我喜欢选择System.Xml.Linq也让喜欢漂亮的Linq类XElement,而不是XmlElement。


Still stuck?
还是卡住了?
Tip: I always prefer to create a dedicated DLL just for the service reference. It can help if you need to wipe it out and start over, and it avoids certain chicken-and-egg compile problems once in a while.
If you end up with half a References.cs file you may be 'reusing a referenced type' that is not compatible with your data contract. i.e. you've added data members on the server side, or changed the signature of an existing member such as making a value type optional.
First, realize that SVCUTIL will quite happily generate an incomplete output file even if it has problems, and when running from Visual Studio you don't get the log file. Keep an eye in Explorer for the expected size and compare it to your 'last known good' size.
Try to run SVCUTIL.EXE directly from a batch file (remember to save this file for next time)
This is easiest to do in a Visual Studio Command prompt
Sample command is as follows, note the
referenceparameter to the DLL that you are referencing types from.svcutil.exe http://dev.example.com/SSWPF.Web/Services/SS.svc/reference:bin\debug\RRStore.Sys.DLL
提示:我总是喜欢为服务引用创建一个专用的 DLL。如果您需要清除它并重新开始,它会有所帮助,并且它偶尔会避免某些先有鸡还是先有蛋的编译问题。
如果您最终得到半个 References.cs 文件,您可能正在“重用引用类型”,该类型与您的数据协定不兼容。即您在服务器端添加了数据成员,或更改了现有成员的签名,例如将值类型设为可选。
首先,意识到 SVCUTIL 会很高兴地生成一个不完整的输出文件,即使它有问题,而且当从 Visual Studio 运行时,你不会得到日志文件。在资源管理器中密切关注预期尺寸,并将其与您的“最后已知的良好”尺寸进行比较。
尝试直接从批处理文件运行 SVCUTIL.EXE(记住保存此文件以备下次使用)
这在 Visual Studio 命令提示符中最容易做到
示例命令如下,请注意
reference要从中引用类型的 DLL的参数。svcutil.exe http://dev.example.com/SSWPF.Web/Services/SS.svc/reference:bin\debug\RRStore.Sys.DLL
Detail: An exception was thrown while running a WSDL import extension:System.ServiceModel.Description.DataContractSerializerMessageContractImporter Error: Referenced type 'SS.Sys.ShippingRateInfo, RRStore.Sys, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' with data contract name 'ShippingRateInfo' in namespace 'http://schemas.datacontract.org/2004/07/SS.Sys' cannot be used since it does not match imported DataContract. Need to exclude this type from referenced types. XPath to Error Source: //wsdl:definitions[@targetNamespace='http://tempuri.org/']/wsdl:portType[@name='ISSWCF']
Detail: An exception was thrown while running a WSDL import extension:System.ServiceModel.Description.DataContractSerializerMessageContractImporter 错误:引用类型“SS.Sys.ShippingRateInfo、RRStore.Sys、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null”,命名空间“ http:// schemas.datacontract.org/2004/07/SS.Sys' 不能使用,因为它与导入的 DataContract 不匹配。需要从引用类型中排除此类型。错误源的 XPath://wsdl:definitions[@targetNamespace=' http://tempuri.org/']/wsdl:portType[@name='ISSWCF']
Fortunately the answer here was simple, my type ShippingRateInfohad changed and I hadn't updated it. Once I copied this type over from the server everything compiled just fine (I chose to revert back to VS tool).
幸运的是,这里的答案很简单,我的类型ShippingRateInfo已经改变,我没有更新它。一旦我从服务器复制了这个类型,一切都编译得很好(我选择恢复到 VS 工具)。
回答by Kevin
The real answer is, if you are serializing a type using the KnownTypeAttribute on your service contract, you MUST include a reference to your type's library in the project you are adding the service reference to.
真正的答案是,如果您使用服务合同上的 KnownTypeAttribute 序列化类型,则必须在要向其添加服务引用的项目中包含对类型库的引用。
For example, if your wcf service serializes the type System.Drawing.Image, then the consumer project MUST have a reference to System.Drawing. Hope this helps some folks out there.
例如,如果您的 wcf 服务序列化 System.Drawing.Image 类型,则使用者项目必须具有对 System.Drawing 的引用。希望这可以帮助那里的一些人。
回答by TrueEddie
I encountered the same issue. Turns out my project was referencing a DLL directly instead of a project reference. So even though my project had a reference for the assembly, it was to an old version. Once I updated the DLL and updated the service reference everything was working again.
我遇到了同样的问题。原来我的项目是直接引用 DLL 而不是项目引用。所以即使我的项目有一个程序集的参考,它也是一个旧版本。一旦我更新了 DLL 并更新了服务引用,一切又恢复了。
回答by arif.khan.b
I'd faced similar issue this is due to type mismatch. Because of which I was not able to generate the client in the test project. We maintain different versions of contracts, while creating new version I'd introduced type mismatch error. Following was the code scenario in my case.
由于类型不匹配,我遇到了类似的问题。因此我无法在测试项目中生成客户端。我们维护不同版本的合约,在创建新版本时我引入了类型不匹配错误。以下是我的案例中的代码场景。
Version 1 Contract
版本 1 合约
[DataContract(Namespace="http://www.exmample.com/v1")]
public enum Fruits
{
[EnumMember]
Apple,
[EnumMember]
Orange
}
Version 2 Contract
第 2 版合约
[DataContract(Namespace="http://www.exmample.com/v1")]
public enum Fruits
{
[EnumMember]
Apple,
[EnumMember]
Orange,
[EnumMember]
Mango
}
I've resolved this issue using svcutilcommand line utility. Command
我已经使用svcutil命令行实用程序解决了这个问题。命令
svcutil MyContract.dll
I got the below error message
我收到以下错误消息
DataContract for type 'V2.Fruits' cannot be added to DataContractSet since type 'V1.Fruits with the same data contract name 'Fruits' in namespace 'http://www.exmample.com/v1' is already present and the contracts are not equivalent.
I changed the namespace from version 1 to version 2 and I was able to generate service reference in test project.
我将命名空间从版本 1 更改为版本 2,并且能够在测试项目中生成服务引用。
[DataContract(Namespace="http://www.exmample.com/v2")]
public enum Fruits
{
[EnumMember]
Apple,
[EnumMember]
Orange,
[EnumMember]
Mango
}
Make use of svcutilthis will help to resolve this issue.
使用svcutil这将有助于解决此问题。
回答by rsuharta
This normally happens for adding a service reference that you had added before. In the client config, it still has the relevant servicemodel. Make sure you delete the servicemodel from the client config and then try to re-add the service reference again!
这通常发生在添加您之前添加的服务引用时。在客户端配置中,它仍然具有相关的服务模型。确保从客户端配置中删除服务模型,然后再次尝试重新添加服务引用!
回答by SharpC
Along the lines of @Kevin's answer, I added references to all the projects and DLLs referenced in the service project. The proxy generation was then able to recognise / generate the types required.
沿着@Kevin 的回答,我添加了对服务项目中引用的所有项目和 DLL 的引用。然后代理生成能够识别/生成所需的类型。
Once that's done, you could even then start to remove some and regenerate to exclude the redundant ones.
完成后,您甚至可以开始删除一些并重新生成以排除多余的。

