locked
Need to deploy ASP.NET Web Services in IIS in windows 10 RRS feed

  • Question

  • User-749382042 posted

    HI, 

    It been 2 days i am trying to deploy a web service in IIS on windows 10.  My requirements was to call the web service from Javascript in another domain so i need to use JSONP. I downloaded a sample code from internet and then completed by development with Web Services running in one Visual Studio development environment and web application in another.  I am using Visual Studio 2015 and the web service project i downloaded, i open it as web project and it work OK.   one thing that was in the web services as a special Module class that was needed to make sure that JSONP is working when a call comes in. 

    Now i want to configure it in IIS on windows 10. I did the simple steps and created a website and point it to my directory.   Now my javascript code in web site cannot make connection with web service running in IIS

    I suspect this is something to do with Module that handles JSONP.      I am dying now to get it working by tomorrow so a immediate help will be highly appreciated. 

    Here is my web.config file

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
    <appSettings />
    <connectionStrings />
    <system.web>

    <compilation debug="true" defaultLanguage="c#">
    <assemblies>
    <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
    <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </assemblies>
    </compilation>

    </system.web>
    <system.codedom>
    <compilers>
    <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <providerOption name="CompilerVersion" value="v3.5" />
    <providerOption name="WarnAsError" value="false" />
    </compiler>
    </compilers>
    </system.codedom>

    <system.webServer>
    <httpErrors errorMode="Detailed" />
    <asp scriptErrorSentToBrowser="true" />
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
    <remove name="ScriptModule" />
    <add name="JSONPModule" type="JsonpHttpModule, App_Code" />
    </modules>
    <handlers>
    <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </handlers>
    </system.webServer>
    </configuration>

    Here is my IHTTPHandler code file and my web service had only one web method 

    using System;
    using System.Web;
    using System.IO;
    using System.Text;

    public class JsonpHttpModule : IHttpModule
    {
    private const string JSON_CONTENT_TYPE = "application/json; charset=utf-8";

    #region IHttpModule Members
    public void Dispose()
    {
    }

    public void Init(HttpApplication app)
    {
    app.BeginRequest += OnBeginRequest;
    app.ReleaseRequestState += OnReleaseRequestState;
    }
    #endregion

    bool _Apply(HttpRequest request)
    {
    if (!request.Url.AbsolutePath.Contains(".asmx")) return false;
    if ("json" != request.QueryString.Get("format")) return false;
    return true;
    }

    public void OnBeginRequest(object sender, EventArgs e)
    {
    HttpApplication app = (HttpApplication)sender;

    if (!_Apply(app.Context.Request)) return;

    if (string.IsNullOrEmpty(app.Context.Request.ContentType))
    {
    app.Context.Request.ContentType = JSON_CONTENT_TYPE;
    }
    }

    public void OnReleaseRequestState(object sender, EventArgs e)
    {
    HttpApplication app = (HttpApplication)sender;

    if (!_Apply(app.Context.Request)) return;

    app.Context.Response.Filter = new JsonResponseFilter(app.Context.Response.Filter, app.Context);
    }
    }

    public class JsonResponseFilter : Stream
    {
    private readonly Stream _responseStream;
    private HttpContext _context;

    public JsonResponseFilter(Stream responseStream, HttpContext context)
    {
    _responseStream = responseStream;
    _context = context;
    }

    public override bool CanRead { get { return true; } }

    public override bool CanSeek { get { return true; } }

    public override bool CanWrite { get { return true; } }

    public override long Length { get { return 0; } }

    public override long Position { get; set; }

    public override void Write(byte[] buffer, int offset, int count)
    {
    var b1 = Encoding.UTF8.GetBytes(_context.Request.Params["callback"] + "(");
    _responseStream.Write(b1, 0, b1.Length);
    _responseStream.Write(buffer, offset, count);
    var b2 = Encoding.UTF8.GetBytes(");");
    _responseStream.Write(b2, 0, b2.Length);
    }


    public override void Close()
    {
    _responseStream.Close();
    }

    public override void Flush()
    {
    _responseStream.Flush();
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
    return _responseStream.Seek(offset, origin);
    }

    public override void SetLength(long length)
    {
    _responseStream.SetLength(length);
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
    return _responseStream.Read(buffer, offset, count);
    }
    }

    Shahzad

          

    Monday, March 27, 2017 6:53 PM

All replies

  • User-2057865890 posted

    Hi Shahzad,

      Now my javascript code in web site cannot make connection with web service running in IIS

    Can you provide detailed error message that you are facing? Without it, it is very hard to get what yo need. Take a look at https://www.ibm.com/developerworks/library/wa-aj-jsonp1/

    Best Regards,

    Chris

    Tuesday, March 28, 2017 5:52 AM
  • User-749382042 posted

    Hi Chris,

    Thanks for the reply.   I think the problem is not with Javascript because as i said this is working fine in development mode. That is both projects running in separate Visual Studio projects and running on different ports and JS is making cross platform calls. I guess the problem is deploying in IIS 10 in windows 10.    I read some where that all security related credentials are disabled in newer platforms and think this is something security related. This is because today i cannot even  deploy my second web application which is a .NET framework 4.6 based MVC application.

    So now I am searching links to deploy web applications in IIS 10 on windows 10

    Shahzad Aslam 

    Tuesday, March 28, 2017 7:16 AM
  • User-2057865890 posted

    >> I downloaded a sample code from internet and then completed by development with Web Services running in one Visual Studio development environment and web application in another

    Do you mean you only deploy web service to IIS on windows 10 and call it from web application in another domain?

    >> Now i want to configure it in IIS on windows 10. I did the simple steps and created a website and point it to my directory

    What error did you get from JavaScript? If you access the web service from web browser in the deployed computer and the computer from another domain after deploying, will you get any error?

    In addition, for enabling CORS, do you must use JSONP? Would you mind enabling CORS in asp.net web service? If not, I suggest you refer below link, it also suits for web service.

    # Enabling CORS in WCF

    https://www.codeproject.com/Articles/845474/Enabling-CORS-in-WCF

    Best Regards,

    Chris

    Tuesday, March 28, 2017 8:07 AM