简体   繁体   English

将启用Ajax的WCF服务暴露给C#代码

[英]Exposing a Ajax Enabled WCF Service to C# code

I created a "Ajax enabled WCF service". 我创建了一个“支持Ajax的WCF服务”。 I can consume the service from javascript code and the WebService works as intended. 我可以从javascript代码中使用该服务,并且WebService按预期工作。 But, I want to write ATP's around it. 但是,我想围绕它写ATP。 So, I am adding the webservice as a "Service Reference" into my ATP Project. 所以,我将web服务作为“服务参考”添加到我的ATP项目中。 Then I am using calling the webservice as usual. 然后我像往常一样使用web服务。 It all compiles fine. 这一切都很好。 But when I run the ATP, it throws me back this error 但是当我运行ATP时,它会把这个错误抛回去

"System.InvalidOperationException : Could not find endpoint element with name 'ServiceReference1.IWCFService' and contract 'ServiceReference1.IWCFService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element." “System.InvalidOperationException:找不到名称为'ServiceReference1.IWCFService'的端点元素,并在ServiceModel客户端配置部分中收缩'ServiceReference1.IWCFService'。这可能是因为没有找到您的应用程序的配置文件,或者因为没有端点元素匹配这个名字可以在客户端元素中找到。“

This is what I have in my App.config file of the ATP. 这就是我在ATP的App.config文件中的内容。

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IWCFService" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://yashworkspace/BSS/WS/ServiceReference1.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IWCFService"
            contract="ServiceReference1.IWCFService" name="BasicHttpBinding_IWCFService" />
    </client>
<services>
    <service name="ServiceReference1.WCFService">
        <endpoint address="http://yashworkspace/BSS/WS/ServiceReference1.svc" behaviorConfiguration="org.proj.WebServices.WCFServiceAspNetAjaxBehavior"
                binding="webHttpBinding" contract="org.proj.WebServices.IWCFService" />
    </service>
</services>
<behaviors>
    <endpointBehaviors>
        <behavior name="org.proj.WebServices.WCFServiceAspNetAjaxBehavior">
            <enableWebScript />
        </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
        <behavior name="">
            <serviceMetadata httpGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
    </serviceBehaviors>
</behaviors>
</system.serviceModel>

Please let me know what could have gone wrong?? 请让我知道可能出了什么问题? I have a normal WCF Service and that is working totally fine. 我有一个正常的WCF服务,这是完全正常的。

Please help. 请帮忙。

regards Yash 关于亚什

The above answer from Drew Marsh is partially right. Drew Marsh的上述答案部分正确。 But the main issue is solved as follows. 但主要问题解决如下。 Additionally, Here the issue is, when u expose the WCF as a ajax enabled WCF service, it communicates using JSON. 此外,问题是,当您将WCF公开为启用了ajax的WCF服务时,它使用JSON进行通信。 When you are calling the same service from a C# code, it called it using a SOAP request. 当您从C#代码调用相同的服务时,它使用SOAP请求调用它。 So you need to configure your service such that it accepts both JSON and SOAP requests. 因此,您需要配置服务,使其接受JSON和SOAP请求。 So, in the services section, you need to configure it as follows 因此,在服务部分中,您需要按如下方式对其进行配置

<services>
      <service name="ServiceReference1.WCFService">
          <endpoint address="" behaviorConfiguration="org.proj.WebServices.WCFServiceAspNetAjaxBehavior"
            binding="webHttpBinding" contract="org.proj.WebServices.IWCFService"  />
          <endpoint address="soapreq" behaviorConfiguration="org.proj.WebServices.MyServiceSOAPBehaviour" bindingConfiguration="BasicHttpBinding_IUpdateService"
                  binding="basicHttpBinding" contract="org.proj.WebServices.IWCFService" name="ServiceReference1.IWCFService" />
      </service>
  </services>

In the behaviors section, you need to configure your endpoint behaviors as follows.. 在behavior部分中,您需要按如下方式配置端点行为。

<endpointBehaviors>
          <behavior name="org.proj.WebServices.WCFServiceAspNetAjaxBehavior">
              <enableWebScript />
          </behavior>
          <behavior name="org.proj.WebServices.MyServiceSOAPBehaviour">
          </behavior>
      </endpointBehaviors>

The problem is the name of the endpoint does not match what the client instance is looking for: 问题是端点的名称与客户端实例正在查找的内容不匹配:

<endpoint address="http://yashworkspace/BSS/WS/ServiceReference1.svc" 
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IWCFService" 
        contract="ServiceReference1.IWCFService" name="BasicHttpBinding_IWCFService" /> 

According to the error message it expects the name to also be "ServiceReference1.IWCFService" so you this is the endpoint you would need. 根据错误消息,它希望该名称也是"ServiceReference1.IWCFService"因此您将需要此端点。

<endpoint address="http://yashworkspace/BSS/WS/ServiceReference1.svc" 
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IWCFService" 
        contract="ServiceReference1.IWCFService" name="ServiceReference1.IWCFService" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM