如何从Javascript服务wcf全部?

时间:2015-11-23 11:30:57

标签: javascript asp.net wcf

Hello Friend我是新手在WCF中,我已经成功创建了WCF服务,我从WPF Windows应用程序以及ASP.Net Web应用程序(JavaScript)调用它,我已成功在WPF Windows应用程序中实现它但遇到问题在使用JavaScript调用ASP.Net Web应用程序。

我的代码如下。

    //Interface
    [ServiceContract]
    public interface Iwcf_Remote_Service
    {
        [OperationContract]
        [WebGet(UriTemplate = "/Subscribe?username={username}",   ResponseFormat =   WebMessageFormat.Json)]
        void Subscribe(string username);
    }

    // Class File
    public class wcf_Remote_Service : Iwcf_Remote_Service
    {

       public void Subscribe(string userName)
       {
          clientID = userName;
          Dictionary3_DE<string, string, DateTime> _Element_DE = Connected_Client_DE.getBy_Key_DE(clientID);
          if (_Element_DE != null)
          {
              _Element_DE.Value_DE = DateTime.Now;
          }
      }
    }
    //Web.Config File
                        <?xml version="1.0"?>
                <!--
                     For more information on how to configure your ASP.NET application, please visit
                     http://go.microsoft.com/fwlink/?LinkId=169433
                  -->
                <configuration>
                  <configSections/>
                  <system.web>
                    <compilation debug="true" targetFramework="4.5.1"/>
                    <httpRuntime targetFramework="4.5.1"/>
                  </system.web>
                  <system.serviceModel>
                    <bindings>
                      <basicHttpBinding>
                        <binding name="DE_Remote_Service_Config" closeTimeout="01:01:00"
                          openTimeout="01:01:00" receiveTimeout="01:10:00" sendTimeout="01:01:00"
                          allowCookies="false" bypassProxyOnLocal="false" maxBufferPoolSize="5242880"
                          maxReceivedMessageSize="20000000" useDefaultWebProxy="true">
                          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
                        </binding>
                        <binding name="BasicHttpBinding_Iwcf_Remote_Service" />
                      </basicHttpBinding>
                    </bindings>
                    <behaviors>
                      <serviceBehaviors>
                        <behavior name="">
                          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                          <serviceDebug includeExceptionDetailInFaults="false" />
                        </behavior>
                        <behavior name="MyRemoteTypeBehaviors">
                          <serviceMetadata httpGetEnabled="false"/>
                          <serviceDebug includeExceptionDetailInFaults="true"/>
                          <serviceThrottling maxConcurrentCalls="200" maxConcurrentSessions="200" maxConcurrentInstances="200"/>
                        </behavior>
                      </serviceBehaviors>
                      <!--<endpointBehaviors>
                        <behavior name="endPointBehavior">
                          <enableWebScript />
                        </behavior>
                      </endpointBehaviors>-->
                    </behaviors>
                    <services>
                      <service behaviorConfiguration="MyRemoteTypeBehaviors" name="DE.WebClient.wcf_Remote_Service">
                        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="DE_Remote_Service_Config" contract="DE.WebClient.Iwcf_Remote_Service"/>
                        <!--<endpoint address="" binding="webHttpBinding" contract="DE.WebClient.Iwcf_Remote_Service" behaviorConfiguration="endPointBehavior"/>-->
                        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
                      </service>
                    </services>
                    <serviceHostingEnvironment aspNetCompatibilityEnabled="true">
                    </serviceHostingEnvironment>
                    <client>
                      <endpoint address="http://localhost:59297/wcf_Remote_Service.svc"
                        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_Iwcf_Remote_Service"
                        contract="wcf_RemoteService_Reference.Iwcf_Remote_Service" name="BasicHttpBinding_Iwcf_Remote_Service" />
                    </client>
                  </system.serviceModel>
                </configuration>


  //Calling From Javascript
  function getAuthentication(_username)
    {
        $.ajax({
            type: "GET",
            url: "wcf_Remote_Service.svc/Subscribe",
            data: '{"userName":"' + _username + '"}',
            dataType: "json",
            processData: false,
            contentType: "text/xml; charset=utf-8",
            success: function (data) {
                alert("true");
            },
            error: function (result) {
                alert(result.d);
            }
        });
    }

0 个答案:

没有答案