svcutil 无法为自托管 wcf 服务生成接口
Posted
技术标签:
【中文标题】svcutil 无法为自托管 wcf 服务生成接口【英文标题】:svcutil failure to generate interfaces for self hosted wcf service 【发布时间】:2019-03-02 06:54:36 【问题描述】:你能帮我解决我的错误吗?
我在 WinForm 程序集中自行托管 WCF,我正在尝试生成客户端 app.config 和服务接口。我在 VS2015 开发者命令提示符下执行 svcutil http://localhost:8880/MyOwnWCFService/ /Language=c# /t:Code /out:C:\Service\ServiceProxy.cs /config:C:\Service\ServiceProxy.config 。
我收到以下错误:
错误:无法从http://localhost:8880/MyOwnWCFService/获取元数据
如果这是您有权访问的 Windows (R) Communication Foundation 服务,请检查您是否已在指定地址启用元数据发布。如需启用元数据发布的帮助,请参阅位于 http://go.microsoft.com/fwlink/?LinkId=65455 的 MSDN 文档。
WS-元数据交换错误 网址:http://localhost:8880/MyOwnWCFService/
Metadata contains a reference that cannot be resolved: 'http://localhost:8880/MyOwnWCFService/'.
Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:8880/MyOwnWCFService/. The client and service bindings may be mismatched.
The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..
HTTP GET 错误 网址:http://localhost:8880/MyOwnWCFService/
There was an error downloading 'http://localhost:8880/MyOwnWCFService/'.
请求失败并显示错误消息:
#content 字体大小:0.7em;垫底:2em; MARGIN-LEFT: 30pxBODYMARGIN-TOP: 0px;左边距:0px;颜色:#000000;字体家族:Verdana;背景颜色:白色P边距顶部:0px;边距底部:12px;颜色:#000000; FONT-FAMILY: VerdanaPREBORDER-RIGHT: #f0f0e0 1px solid;填充右:5px;边框顶部:#f0f0e0 1px 实心;边距顶部:-5px;填充左:5px;字体大小:1.2em;填充底部:5px;左边界:#f0f0e0 1px 实心;填充顶部:5px;边框底部:#f0f0e0 1px 实心; FONT-FAMILY:Courier New;背景颜色:#e5e5cc.heading1MARGIN-TOP:0px;填充左:15px;字重:正常;字体大小:26px;边距底部:0px;填充底部:3px;左边距:-30px;宽度:100%;颜色:#ffffff;填充顶部:10px;字体家族:Tahoma;背景颜色:#003366.introMARGIN-LEFT:-15pxServiceService
由于内部错误,服务器无法处理请求。有关该错误的更多信息,请在服务器上打开 IncludeExceptionDetailInFaults(来自 ServiceBehaviorAttribute 或来自配置行为)以便将异常信息发送回客户端,或者根据 Microsoft .NET Framework SDK 文档打开跟踪并检查服务器跟踪日志。 --.如果您需要更多帮助,请输入“svcutil /?”
我的自托管代码:
System.ServiceModel.Channels.Binding MexHttpBinding = MetadataExchangeBindings.CreateMexHttpBinding();
String baseAddress = "http://localhost:8880/MyOwnWCFService/";
NetTcpBinding tcpbinding = new NetTcpBinding(SecurityMode.None);
tcpbinding.Name = "SEMSIntegration_NetTcpBinding_iSEMSIntegrationWCFService";
tcpbinding.OpenTimeout = new TimeSpan(00, 01, 00);
tcpbinding.CloseTimeout = new TimeSpan(00, 01, 00);
tcpbinding.ReceiveTimeout = new TimeSpan(00, 01, 00);
tcpbinding.SendTimeout = new TimeSpan(00, 01, 00);
tcpbinding.TransactionFlow = false;
tcpbinding.TransferMode = TransferMode.Buffered;
tcpbinding.TransactionProtocol = TransactionProtocol.OleTransactions;
tcpbinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
tcpbinding.ListenBacklog = 10;
tcpbinding.MaxBufferPoolSize = 524288;
tcpbinding.MaxBufferSize = 524288;
tcpbinding.MaxConnections = 100;
tcpbinding.MaxReceivedMessageSize = 524288;
WindowState = FormWindowState.Minimized;
SEMSService = new MyOwnWCFService();
SEMSHost = new ServiceHost(SEMSService, new Uri(baseAddress));
ServiceMetadataBehavior smb = SEMSHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
// If not, add one
if (smb == null)
smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
SEMSHost.Description.Behaviors.Add(smb);
SEMSHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
SEMSHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());
SEMSHost.AddServiceEndpoint(typeof(iSEMSIntegrationWCFService), new BasicHttpBinding(), "http://localhost:8880/MyOwnWCFService");
SEMSHost.AddServiceEndpoint(typeof(iSEMSIntegrationWCFService), tcpbinding, "net.tcp://localhost:8888/MyOwnWCFService");
SEMSHost.AddServiceEndpoint("IMetadataExchange", MexHttpBinding, "mex");
SEMSHost.Open();
【问题讨论】:
我的 WCF 知识有点生疏。如果将 svcutil 命令行更改为http://localhost:8880/MyOwnWCFService/wsdl?
会发生什么?
我几乎得到了同样的错误(太长,无法添加到此评论中)。
【参考方案1】:
This is because you are getting the error:
System.Runtime.Serialization.InvalidDataContractException: Type 'System.Windows.Forms.Menu' cannot be serialized.
See browser exception detail:
An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.ServiceModel.Description.DataContractSerializerOperationBehavior
contract: http://tempuri.org/:iSEMSIntegrationWCFService ----> System.Runtime.Serialization.InvalidDataContractException: Type 'System.Windows.Forms.Menu' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.ThrowInvalidDataContractException(String message, Type type)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
at System.Runtime.Serialization.ClassDataContract.ClassDataContractCriticalHelper..ctor(Type type)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id, RuntimeTypeHandle typeHandle, Type type)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidation(Int32 id, RuntimeTypeHandle typeHandle, Type type)
at System.Runtime.Serialization.DataContractSet.GetDataContract(Type clrType)
at System.Runtime.Serialization.DataContractSet.GetMemberTypeDataContract(DataMember dataMember)
at System.Runtime.Serialization.DataContractSet.AddClassDataContract(ClassDataContract classDataContract)
at System.Runtime.Serialization.DataContractSet.InternalAdd(XmlQualifiedName name, DataContract dataContract)
at System.Runtime.Serialization.DataContractSet.AddClassDataContract(ClassDataContract classDataContract)
at System.Runtime.Serialization.DataContractSet.InternalAdd(XmlQualifiedName name, DataContract dataContract)
at System.Runtime.Serialization.DataContractSet.AddClassDataContract(ClassDataContract classDataContract)
at System.Runtime.Serialization.DataContractSet.InternalAdd(XmlQualifiedName name, DataContract dataContract)
at System.Runtime.Serialization.DataContractSet.AddClassDataContract(ClassDataContract classDataContract)
at System.Runtime.Serialization.DataContractSet.InternalAdd(XmlQualifiedName name, DataContract dataContract)
at System.Runtime.Serialization.DataContractSet.AddClassDataContract(ClassDataContract classDataContract)
at System.Runtime.Serialization.DataContractSet.InternalAdd(XmlQualifiedName name, DataContract dataContract)
at System.Runtime.Serialization.DataContractSet.AddClassDataContract(ClassDataContract classDataContract)
at System.Runtime.Serialization.DataContractSet.InternalAdd(XmlQualifiedName name, DataContract dataContract)
at System.Runtime.Serialization.DataContractSet.Add(Type type)
at System.Runtime.Serialization.XsdDataContractExporter.Export(Type type)
at System.ServiceModel.Description.MessageContractExporter.ExportType(Type type, String partName, String operationName, XmlSchemaType& xsdType)
at System.ServiceModel.Description.DataContractSerializerMessageContractExporter.ExportBody(Int32 messageIndex, Object state)
at System.ServiceModel.Description.MessageContractExporter.ExportMessage(Int32 messageIndex, Object state)
at System.ServiceModel.Description.MessageContractExporter.ExportMessageContract()
at System.ServiceModel.Description.DataContractSerializerOperationBehavior.System.ServiceModel.Description.IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext contractContext)
at System.ServiceModel.Description.WsdlExporter.CallExtension(WsdlContractConversionContext contractContext, IWsdlExportExtension extension)
--- End of inner ExceptionDetail stack trace ---
at System.ServiceModel.Description.WsdlExporter.CallExtension(WsdlContractConversionContext contractContext, IWsdlExportExtension extension)
at System.ServiceModel.Description.WsdlExporter.CallExportContract(WsdlContractConversionContext contractContext)
at System.ServiceModel.Description.WsdlExporter.ExportContract(ContractDescription contract)
at System.ServiceModel.Description.WsdlExporter.ExportEndpoint(ServiceEndpoint endpoint, XmlQualifiedName wsdlServiceQName, BindingParameterCollection bindingParameters)
at System.ServiceModel.Description.WsdlExporter.ExportEndpoints(IEnumerable`1 endpoints, XmlQualifiedName wsdlServiceQName, BindingParameterCollection bindingParameters)
at System.ServiceModel.Description.ServiceMetadataBehavior.MetadataExtensionInitializer.GenerateMetadata()
at System.ServiceModel.Description.ServiceMetadataExtension.EnsureInitialized()
at System.ServiceModel.Description.ServiceMetadataExtension.get_Metadata()
at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.InitializationData.InitializeFrom(ServiceMetadataExtension extension)
at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.GetInitData()
at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.TryHandleDocumentationRequest(Message httpGetRequest, String[] queries, Message& replyMessage)
at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.ProcessHttpRequest(Message httpGetRequest)
at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.Get(Message message)
at SyncInvokeGet(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
Contract Definition:
<system.serviceModel>
<services>
<service name="myService" behaviorConfiguration="MyDefaultBehaviour">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8880/MyOwnWCFService/" />
</baseAddresses>
</host>
<endpoint
address=""
binding="basicHttpBinding"
contract="iSEMSIntegrationWCFService" />
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetaDataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name ="MyDefaultBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceThrottling maxConcurrentCalls="1" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
【讨论】:
合约定义是什么? 合同定义附加到答案。以上是关于svcutil 无法为自托管 wcf 服务生成接口的主要内容,如果未能解决你的问题,请参考以下文章
WCF:Svcutil 生成无效的客户端代理、Apache AXIS Web 服务、重载操作
WCF通过SVCUtil.exe生成客户端代理类和配置文件(转)