Pages

Thursday, October 1, 2015

.NET Web Configuration - Access Service by both AJAX & SOAP.

Web Configuration
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <connectionStrings>
    <add name="" />
  </connectionStrings>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>


  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="VMBasicHttpBinding" />
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ContactBehavior">
          <serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="True" />
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="SOAPBehavior" />
        <behavior name="AJAXBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="DNS.Service.ContactSearch" behaviorConfiguration="ContactBehavior">
        <endpoint address="" binding="webHttpBinding" behaviorConfiguration="AJAXBehavior" name="webEndPoint" contract="DNS.Service.IContactSearch" />
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
        <endpoint address="soapreq" behaviorConfiguration="SOAPBehavior" bindingConfiguration="VMBasicHttpBinding" binding="basicHttpBinding" contract="DNS.Service.IContactSearch" name="DNS.Service.IContactSearch" />
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <directoryBrowse enabled="true" />
  </system.webServer>
</configuration>

AJAX Call
    <script>
        var iurl = "//<URL>/DNS.ContactManagement.Service/ContactSearch.svc/StreetName";
    var idata = JSON.stringify({ loginUserGuid: "12345678-1234-1234-1234-12345678", name: $('#txtStreetName').val() });
            $.ajax({
                url: iurl,
                data: idata,
                type: "POST",
                dataType: "text",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    //alert(XMLHttpRequest.responseText);
                }
            });
    </script>

SOAP C# Code
try
    {
url = "http://<url>/DNS.ContactManagement.Service/ContactSearch.svc/soapreq";
BasicHttpBinding bhBinding = new BasicHttpBinding();
bhBinding.Name = "ContactSearch";
bhBinding.Security.Mode = BasicHttpSecurityMode.None;
bhBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
bhBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
bhBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
EndpointAddress eAddress = new EndpointAddress(url);

ContactSearchSerRef.ContactSearchClient vsclnt = new ContactSearchSerRef.ContactSearchClient(bhBinding, eAddress);
vsclnt.GenerateXML(loginUserId, xmlName);
    }
    catch (Exception ex)
    {
throw new Exception(ex.ToString());
    }

No comments:

Post a Comment