简体   繁体   English

手动调用安全WCF服务

[英]Calling secure WCF service manually

I need to call a WCF manually (via HttpWebRequest). 我需要手动调用WCF(通过HttpWebRequest)。 I can call my service by adding a web reference and calling it through the proxy, so I know the service is setup correctly and the certs, web config, etc. is all correct. 我可以通过添加Web参考并通过代理对其进行调用来调用我的服务,因此我知道该服务已正确设置,并且证书,Web配置等均正确。 Based on samples I've found, I think I'm doing it correctly, but still getting an internal error 500. 根据我发现的样本,我认为我做得正确,但仍然收到内部错误500。

EDIT: WebService is using wsHttpBinding. 编辑:WebService正在使用wsHttpBinding。

Console app code is: 控制台应用程序代码为:

    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 is the XML request... just hard coding for now: 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>

What am I missing here? 我在这里想念什么? There are no further details inside the exception. 异常内没有更多详细信息。

EDIT: 编辑:

Web.config: 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