簡體   English   中英

手動調用安全WCF服務

[英]Calling secure WCF service manually

我需要手動調用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>

您需要使用HttpWebBinding來通過Http訪問任何WCF服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM