Home Articles

从经典ASP调用WCF服务

Asked
Viewed 1267 times
1

我在使用传统ASP调用WCF服务中的方法时遇到问题 . 以下是服务和配置的代码:

Service

[ServiceContract(Namespace = "http://Plumtree.dartKW.PdfGenerator.Web")]
public interface IPdfGeneratorService
{
    [OperationContract]
    String GeneratePdf(String xml);
}

public class PdfGeneratorService : IPdfGeneratorService
{
    #region IPdfGeneratorService Members

    public String GeneratePdf(String xml)
    {
        return "hello";
    }

    #endregion
}

Configuration

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="BasicHttpBinding_PdfGenerator"
               maxBufferSize="2147483647"
               maxReceivedMessageSize="2147483647"
               maxBufferPoolSize="2147483647"
               transferMode="Buffered">
        <readerQuotas
          maxArrayLength="2147483647"
          maxStringContentLength="2147483647"
          maxBytesPerRead="2147483647"
          maxNameTableCharCount="2147483647"
          maxDepth="32" />
      </binding>
    </basicHttpBinding>
  </bindings>
  <services>
    <service behaviorConfiguration="serviceBehavior"
             name="Plumtree.dartKW.PdfGenerator.Web.PdfGeneratorService">
      <endpoint binding="basicHttpBinding"
                bindingName="BasicHttpBinding_PdfGenerator"
                contract="Plumtree.dartKW.PdfGenerator.Web.IPdfGeneratorService" />
      <endpoint address="mex"
                binding="mexHttpBinding"
                contract="IMetadataExchange" />
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="serviceBehavior">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

我在WCF测试客户端测试了上面的方法,它工作正常 . 这是我的asp页面中的代码:

mexMonikerString = "service:mexAddress='http://localhost:51997/PdfGeneratorService.svc/mex'"
mexMonikerString = mexMonikerString + ", address='http://localhost:51997/PdfGeneratorService.svc'"
mexMonikerString = mexMonikerString + ", binding=basicHttpBinding"
mexMonikerString = mexMonikerString + ", bindingNamespace='http://tempuri.org/'"
mexMonikerString = mexMonikerString + ", contract=IPdfGeneratorService"
mexMonikerString = mexMonikerString + ", contractNamespace='http://Plumtree.dartKW.PdfGenerator.Web'"

dim service
service = GetObject(mexMonikerString)

每当我尝试这个时,我都会收到以下错误:

System.ServiceModel错误'800401e4' Contract 没有支持指定绑定的 endpoints .

我已经尝试在名字对象中传递绑定配置名称 BasicHttpBinding_PdfGenerator 而不是 basicHttpBinding 但是我得到了同样的错误 . 我也找不到任何其他在线错误的例子 .

任何帮助将不胜感激

1 Answer

  • 0

    您可以使用JavaScript轻松完成 . 这是我在一个简单的HTML页面中嵌入的一些JavaScript的副本,我将它提供给我的WCF Web服务用户 .

    困难的部分可能是弄清楚WCF服务在请求中期望什么 . 如果您有权访问WCF服务代码,那么很容易理解 . 另一种方法是使用Fiddler捕获客户端和wcf主机之间发送的成功消息 . 或WSDL,如果你可以生成一个 .

    祝好运 .

    function Ping() {             
    //set up varable
    var sContent;
    // 'Content-Type: text/xml \r\n ' +
    // 'Host: localhost:8085 \r\n \r\n  ';
    sContent=  "<xml>Your message matching the web service's data or message contract</xml>";
    
    var xmlhttp =  new XMLHttpRequest();
    xmlhttp.open('POST', Demo.URL.value, true);  
    xmlhttp.onreadystatechange = function() {
    //this section acts as a listener, waiting for response from host
    if (xmlhttp.readyState == 4||xmlhttp.readyState == 0) {
        //alert("Ready state: " + xmlhttp.readyState.toString());
        if (xmlhttp.status == 200) {
            //alert("good");
            Demo.pingresponse.value = "Response: " +xmlhttp.responseText;
        }
        if (xmlhttp.status !=200){
            //alert("bad");
            Demo.pingresponse.value = "Error: " +xmlhttp.status.toString() +"  response text:  " +xmlhttp.responseText;
        }
    } else {
        //alert("readystate bad");
    }
     }
     //send request    
    
    xmlhttp.setRequestHeader("POST http:localhost:8085/HostInterface/HostConnect HTTP/1.1"); 
    xmlhttp.setRequestHeader("VsDebuggerCausalityData",""); 
    xmlhttp.setRequestHeader   ("SOAPAction","\"http://somestuff\""); 
    xmlhttp.setRequestHeader("Host","localhost:8085"); 
    xmlhttp.setRequestHeader("Expect","100-continue"); 
    xmlhttp.setRequestHeader("Accept-Encoding","gzip, deflate"); 
    xmlhttp.setRequestHeader("Connection","Keep-Alive"); 
    xmlhttp.setRequestHeader("Content-Length","639");                   
    xmlhttp.setRequestHeader("Content-type", "text/xml; charset=utf-8"); 
    xmlhttp.send(sContent);                  
    }
    

Related