【问题标题】:Calling secure WCF service manually手动调用安全 WCF 服务
【发布时间】:2016-02-21 06:29:35
【问题描述】:

我需要手动调用 WCF(通过 HttpWebRequest)。我可以通过添加 Web 引用并通过代理调用它来调用我的服务,因此我知道服务设置正确,并且证书、Web 配置等都是正确的。根据我找到的示例,我认为我做得正确,但仍然收到内部错误 500。

编辑:WebService 正在使用 wsHttpBinding。

控制台应用代码是:

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1");
    string strRequest = Properties.Resources.TextFile1;

    req.Method = "POST";
    req.ContentType = "application/soap+xml; charset=utf-8";
    req.ContentLength = strRequest.Length;
    req.Credentials = new NetworkCredential("test", "test");

    using (Stream stream = req.GetRequestStream())
    {
        stream.Write(UTF8Encoding.Default.GetBytes(strRequest), 0, strRequest.Length);
    }

    using (WebResponse res = req.GetResponse())
    {
        // nothing to do here
    }

TextFile1 是 XML 请求...现在只是硬编码:

<s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope">
  <s:Header>
    <a:Action s:mustUnderstand="1">http://tempuri.org/IService1/GetData</a:Action>
    <a:MessageID>urn:uuid:f3c2172e-eeb9-4dfd-8e1b-3a623088b78f</a:MessageID>
    <a:ReplyTo>
      <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
    </a:ReplyTo>
  </s:Header>
  <s:Body>
    <GetData xmlns="http://tempuri.org/">
      <value>0</value>
      <value2>test</value2>
    </GetData>
  </s:Body>
</s:Envelope>

我在这里缺少什么?异常中没有更多细节。

编辑:

Web.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.Service1">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="RequestUserName" contract="WcfServiceLibrary1.IService1">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
      <bindings>
          <wsHttpBinding>
              <binding name="RequestUserName">
                  <security mode="Message">
                      <message clientCredentialType="UserName"/>
                  </security>
              </binding>
          </wsHttpBinding>
      </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="True" />
            <serviceCredentials>
                <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="WcfServiceLibrary1.DistributorValidator, WcfServiceLibrary1" />
                <serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="TrustedPeople" x509FindType="FindBySubjectName"/>
            </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

【问题讨论】:

  • 你能分享你的web.config吗?
  • @SohamDasgupta - 嗨。添加了 Web.config。标准的东西。我确实看到状态码是“ProtocolError”。
  • 您能否使用 SoapUI 访问该服务?如果是这样,请将其与您正在使用的标头/方法进行比较。
  • 我不认为这可能是你试图实现的方式......你必须使用 ChannelFactory 从客户端调用 WCf 服务......但是为什么不使用代理呢?
  • @Viru,我需要能够动态调用 any Web 服务(用户指定 URL、命名空间、参数等)。我可以使用 HttpWebBinding(不安全)来做到这一点,但我们想添加对 wsHttpBinding(安全)的支持。

标签: c# .net wcf


【解决方案1】:

您需要使用HttpWebBinding 通过Http 访问任何WCF 服务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-13
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多