Host a Wcf Service Library in a Web Application

By : Jni
Source: Stackoverflow.com
Question!

I would like to host a Wcf Service, create in a Wcf service Library, in a Web Application.

I've already done the same thing with Web Service asmx :

using System.Reflection;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace WebServiceLibrary
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class WebService1 : WebService, IHttpHandlerFactory
    {
        private static WebServiceHandlerFactory wshf = new WebServiceHandlerFactory();
        private static MethodInfo coreGetHandlerMethod = typeof(WebServiceHandlerFactory).GetMethod("CoreGetHandler", BindingFlags.Instance | BindingFlags.NonPublic);

        public System.Web.IHttpHandler GetHandler(System.Web.HttpContext context, string requestType, string url, string pathTranslated)
        {
            return (IHttpHandler)coreGetHandlerMethod.Invoke(wshf, new object[] { GetType(), context, context.Request, context.Response });
        }

        public void ReleaseHandler(IHttpHandler handler)
        {

        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

The web.config file of the Web Application :

    <system.webServer>
      <handlers>
          <add name="WebService" path="WebService1.asmx" verb="*" preCondition="integratedMode" type="WebServiceLibrary.WebService1"/>
      </handlers>
    </system.webServer>

Is here a way to do the same thing with a Wcf Service ?

Regards.

By : Jni


Answers

Sure - the simplest thing would likely be to create a new 'WCF Service Application' (which is a webapp) and use that as a template in terms of what bits you would need, for instance the system.serviceModel section in your web.config





This video can help you solving your question :)
By: admin