需要有关支持 AJAX 的 WCF 服务的帮助(请查看 Web.Config 中的 @System.ServiceModel)

发布于 2024-07-29 13:55:22 字数 9987 浏览 8 评论 0原文

这几天我一直遇到这个问题...... 我设计了一个WCF类库-WebAPILibrary。 它在我的下面 解决方案,其中还包含我的网络项目。

我现在尝试启用对搜索查询的 AJAX/JSON 响应。

在客户端代码示例中,我看到了一些大意:

function btnsrch_onclick() {

                var query = document.getElementById('q'); 
                var type = document.getElementById('type');

              $(document).ready(function(){
                  $.getJSON("http://api.domain.com/services/trade.svc?q=" + query.value + "&type=" + type.value + "&format=json",
                      function(data){
                          $.each(data.items, function(i,item){
                          $("<img/>").attr("src", item.media.m).appendTo("#images");
                              if ( i == 3 ) return false;
                      });
                  });
              });

                }

我的问题是,到目前为止,我一直在我的 Web 项目中愉快地引用我的 WCF 类库。 但是,现在看来我需要创建一个 SVC 文件并用它来 执行我的 javascript AJAX/JSON 调用。

我的问题是:

1) 如何与我的 WCF 类库配合创建 SVC 文件? 2)我不太确定我需要在我的网络项目的 system.ServiceModel 下放置什么 网络配置。 有好心人可以看看我目前拥有的东西并给我建议吗 我需要做什么?

WEB 项目系统.SERVICEMODEL (WEB.CONFIG):

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ITradeService" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
          textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
            enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="Windows" negotiateServiceCredential="true"
              algorithmSuite="Default" establishSecurityContext="true" />
          </security>
        </binding>
        <binding name="WSHttpBinding_IAuthService" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
          textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
            enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="Windows" negotiateServiceCredential="true"
              algorithmSuite="Default" establishSecurityContext="true" />
          </security>
        </binding>
        <binding name="WSHttpBinding_IAuthService1" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
          textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
            enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="Windows" negotiateServiceCredential="true"
              algorithmSuite="Default" establishSecurityContext="true" />
          </security>
        </binding>
        <binding name="WSHttpBinding_ITradeService1" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
          textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
            enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="Windows" negotiateServiceCredential="true"
              algorithmSuite="Default" establishSecurityContext="true" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:8731/Design_Time_Addresses/Trade/"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITradeService"
        contract="ITradeService" name="WSHttpBinding_ITradeService">
        <identity>
          <dns value="localhost:8731" />
        </identity>
      </endpoint>
      <endpoint address="http://localhost:8731/Design_Time_Addresses/Authentication/"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IAuthService"
        contract="IAuthService" name="WSHttpBinding_IAuthService">
        <identity>
          <dns value="localhost:8731" />
        </identity>
      </endpoint>
      <endpoint address="http://localhost:8731/Design_Time_Addresses/Authentication/"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IAuthService1"
        contract="Trezoro.WebAPI.Authentication.IAuthService" name="WSHttpBinding_IAuthService1">
        <identity>
          <dns value="localhost:8731" />
        </identity>
      </endpoint>
      <endpoint address="http://localhost:8731/Design_Time_Addresses/Trade/"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITradeService1"
        contract="Trezoro.WebAPI.Trade.ITradeService" name="WSHttpBinding_ITradeService1">
        <identity>
          <dns value="localhost:8731" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>

WCF 类库系统.SERVICEMODEL (APP.CONFIG):

   <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
        <services>
          <service behaviorConfiguration="WebAPILibrary.WebAPIBehavior"
            name="Company.WebAPI.Trade.TradeService">
            <endpoint address="" binding="wsHttpBinding" contract="Company.WebAPI.Trade.ITradeService">
              <identity>
                <dns value="localhost:8731" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8731/Design_Time_Addresses/Trade/" />
              </baseAddresses>
            </host>
          </service>
          <service behaviorConfiguration="WebAPILibrary.WebAPIBehavior"
            name="Company.WebAPI.Authentication.AuthService">
            <endpoint address="" binding="wsHttpBinding" contract="Company.WebAPI.Authentication.IAuthService">
              <identity>
                <dns value="localhost:8731" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8731/Design_Time_Addresses/Authentication/" />
              </baseAddresses>
            </host>
          </service>
        </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="WebAPILibrary.WebAPIBehavior">
              <enableWebScript/>
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="WebAPILibrary.WebAPIBehavior">
              <!-- To avoid disclosing metadata information, 
              set the value below to false and remove the metadata endpoint above before deployment -->
              <serviceMetadata httpGetEnabled="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" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>

I have been having some problems with this for a few days now...
I have designed a WCF Class Library - WebAPILibrary. It is under my
solution which also contains my web project.

I now am attempting to enable a AJAX/JSON response to a search query.

In the client code examples, I see something to the effect:

function btnsrch_onclick() {

                var query = document.getElementById('q'); 
                var type = document.getElementById('type');

              $(document).ready(function(){
                  $.getJSON("http://api.domain.com/services/trade.svc?q=" + query.value + "&type=" + type.value + "&format=json",
                      function(data){
                          $.each(data.items, function(i,item){
                          $("<img/>").attr("src", item.media.m).appendTo("#images");
                              if ( i == 3 ) return false;
                      });
                  });
              });

                }

MY PROBLEM IS that, up until this point, I have been happily referencing my WCF class library in my Web Project. But, now it seems I need to create a SVC file and use it to
execute my javascript AJAX/JSON calls.

MY QUESTION IS:

1) How do I create the SVC file in cooperation with my WCF class library?
2) I am not quite sure what I need to put under system.ServiceModel for my web project's
web.config. Could some kind soul kindly look at what I currently have and advise me on
what I need to be doing?

WEB PROJECT SYSTEM.SERVICEMODEL (WEB.CONFIG):

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ITradeService" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
          textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
            enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="Windows" negotiateServiceCredential="true"
              algorithmSuite="Default" establishSecurityContext="true" />
          </security>
        </binding>
        <binding name="WSHttpBinding_IAuthService" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
          textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
            enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="Windows" negotiateServiceCredential="true"
              algorithmSuite="Default" establishSecurityContext="true" />
          </security>
        </binding>
        <binding name="WSHttpBinding_IAuthService1" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
          textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
            enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="Windows" negotiateServiceCredential="true"
              algorithmSuite="Default" establishSecurityContext="true" />
          </security>
        </binding>
        <binding name="WSHttpBinding_ITradeService1" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
          textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
            enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="Windows" negotiateServiceCredential="true"
              algorithmSuite="Default" establishSecurityContext="true" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:8731/Design_Time_Addresses/Trade/"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITradeService"
        contract="ITradeService" name="WSHttpBinding_ITradeService">
        <identity>
          <dns value="localhost:8731" />
        </identity>
      </endpoint>
      <endpoint address="http://localhost:8731/Design_Time_Addresses/Authentication/"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IAuthService"
        contract="IAuthService" name="WSHttpBinding_IAuthService">
        <identity>
          <dns value="localhost:8731" />
        </identity>
      </endpoint>
      <endpoint address="http://localhost:8731/Design_Time_Addresses/Authentication/"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IAuthService1"
        contract="Trezoro.WebAPI.Authentication.IAuthService" name="WSHttpBinding_IAuthService1">
        <identity>
          <dns value="localhost:8731" />
        </identity>
      </endpoint>
      <endpoint address="http://localhost:8731/Design_Time_Addresses/Trade/"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITradeService1"
        contract="Trezoro.WebAPI.Trade.ITradeService" name="WSHttpBinding_ITradeService1">
        <identity>
          <dns value="localhost:8731" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>

WCF CLASS LIBRARY SYSTEM.SERVICEMODEL (APP.CONFIG):

   <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
        <services>
          <service behaviorConfiguration="WebAPILibrary.WebAPIBehavior"
            name="Company.WebAPI.Trade.TradeService">
            <endpoint address="" binding="wsHttpBinding" contract="Company.WebAPI.Trade.ITradeService">
              <identity>
                <dns value="localhost:8731" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8731/Design_Time_Addresses/Trade/" />
              </baseAddresses>
            </host>
          </service>
          <service behaviorConfiguration="WebAPILibrary.WebAPIBehavior"
            name="Company.WebAPI.Authentication.AuthService">
            <endpoint address="" binding="wsHttpBinding" contract="Company.WebAPI.Authentication.IAuthService">
              <identity>
                <dns value="localhost:8731" />
              </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8731/Design_Time_Addresses/Authentication/" />
              </baseAddresses>
            </host>
          </service>
        </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="WebAPILibrary.WebAPIBehavior">
              <enableWebScript/>
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="WebAPILibrary.WebAPIBehavior">
              <!-- To avoid disclosing metadata information, 
              set the value below to false and remove the metadata endpoint above before deployment -->
              <serviceMetadata httpGetEnabled="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" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鲸落 2024-08-05 13:55:22

您永远不应该在任何其他项目中引用您的 WCF 库。 它旨在用作服务,而不是用作类库。

You should never reference your WCF library in any other project. It is meant to be used as a service, not as a class library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文