会感激一些帮助 - 我觉得我必须遗漏一些明显的东西。我想使用System.ServiceModel.Description.MetadataExchangeClient来检索有关Web服务的信息。如果Web服务不安全,那么我没有问题,但如果Web服务是由Windows身份验证保护的WCF服务,那么我会得到一个错误:元数据包含无法解析的引用:'http:/ /server/Service.svc?WSDL”。
我的代码非常简单:
Uri endpointUri = new Uri("http://server/Service.svc?WSDL");
System.ServiceModel.Description.MetadataExchangeClient mexClient = new System.ServiceModel.Description.MetadataExchangeClient(endpointUri, System.ServiceModel.Description.MetadataExchangeClientMode.HttpGet);
mexClient.ResolveMetadataReferences = true;
System.Net.NetworkCredential mexNetworkCredentials = new System.Net.NetworkCredential(username, password, domain);
mexClient.HttpCredentials = mexNetworkCredentials;
System.ServiceModel.Description.MetadataSet metadataSet = mexClient.GetMetadata(endpointUri, System.ServiceModel.Description.MetadataExchangeClientMode.HttpGet);
我是否只是缺少客户端需要指定身份验证的方式?我搜索了一个示例代码,但似乎没有任何东西符合我的想法。
这是Web.config文件(稍微编辑):
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
<connectionStrings>
<remove name="db"/>
<add name="db" connectionString="server="server";database="database";Integrated Security=SSPI;" providerName="System.Data.SqlClient="/>
</connectionStrings>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation>
<assemblies>
<add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
</system.web>
<location path="." inheritInChildApplications="false">
<system.web>
<authentication mode="Windows"/>
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</controls>
</pages>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="ScriptModule"/>
<add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory"/>
<remove name="ScriptHandlerFactoryAppServices"/>
<remove name="ScriptResource"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</handlers>
</system.webServer>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="SecuredHttpBinding" maxReceivedMessageSize="52428800">
<readerQuotas maxArrayLength="52428800"/>
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="APIServiceBehavior" name="APIService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="SecuredHttpBinding" name="IAPIService" contract="IAPIService" bindingNamespace="http://www.general.com/general/api/v1"/>
</service>
<service behaviorConfiguration="APIServiceBehavior" name="Com.IntApp.Walls.API.InternalAPIService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="SecuredHttpBinding" name="IInternalAPIService" contract="IInternalAPIService" bindingNamespace="http://www.general.com/general/api/v1"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="APIServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</location>
</configuration>
感谢任何帮助,马丁
答案 0 :(得分:2)
我会回答我自己的问题,希望以后可以帮助别人。我通过将以下XML块添加到我的App.config文件中来启用对客户端的跟踪:
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "D:\log\Traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
在检查生成的跟踪日志时,我可以看到抛出了异常:首先是因为我的客户端对MaxReceivedMessageSize的设置太低,然后因为MaxNameTableCharCount和MaximumResolvedReferences的默认设置太低。我用以下代码修改了这些代码:
BasicHttpBinding basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
basicHttpBinding.MaxReceivedMessageSize = 52428800;
System.Xml.XmlDictionaryReaderQuotas xmlDictionaryReaderQuotas = new XmlDictionaryReaderQuotas();
xmlDictionaryReaderQuotas.MaxNameTableCharCount = 2147483647;
basicHttpBinding.ReaderQuotas = xmlDictionaryReaderQuotas;
System.ServiceModel.Description.MetadataExchangeClient mexClient = new System.ServiceModel.Description.MetadataExchangeClient(basicHttpBinding);
mexClient.MaximumResolvedReferences = 1000;
System.ServiceModel.Description.MetadataSet metadataSet = mexClient.GetMetadata(endpointUri, System.ServiceModel.Description.MetadataExchangeClientMode.HttpGet);
希望这能节省一些时间!