类库服务寄宿到WebHost

1.该Demo中包含一个类库项目、一个空的WebForm项目

2.新建WebForm项目

3.全局路由中注册类库服务

public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes();


            System.ServiceModel.WebHttpBinding bing = new System.ServiceModel.WebHttpBinding();
            bing.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas() { MaxStringContentLength = 2147483647 }; //(更改这个数字) 
            bing.MaxReceivedMessageSize = 2147483647;
        }
        private void RegisterRoutes()
        {
            RouteTable.Routes.Add(new ServiceRoute("blog", new WebServiceHostFactory(), typeof(ClientApiTest.Class1)));

        }
    }

  

4.新建类库服务项目ClientApiTest

[ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class Class1
    {
        [WebInvoke(Method = "POST", UriTemplate = "blog/publish/micoblog", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public PublishMicoBlogResponse PublishMicoBlog(PublishMicoBlogRequest request)
        {
            PublishMicoBlogResponse re = new PublishMicoBlogResponse();
            return re;
        }
    }

  

5.启动测试(报错,修改配置)

http://localhost:54235/blog/blog/publish/micoblog

 6.WebHost修改配置:

 <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>

  

原文地址:https://www.cnblogs.com/liuqiyun/p/9224070.html