如何访问在 Class 库上创建的 Web 服务?
How Is It Possible to Access WebService Created on a Class Library?
我首先在 ASP.net 项目中创建了一个 Web 服务,然后将其代码移动到一个 class 库到一个 CSharp(.cs) 文件中。
我还在这个新创建的 class 中添加了 "IHttpHandlerFactory" 的实现,以便我可以在 web.config 文件上注册此服务:
public class Test: WebService, IHttpHandlerFactory
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Hello(string world)
{
return "Hello " + world;
}
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)
{
}
}
并在web.config中注册:
<add name="TestService" path="TestService.asmx" verb="*" type="MyApp.Library.Test, MyApp.Library, Version=1.0.0.0, Culture=neutral" preCondition="integratedMode" />
我可以在这里访问它:
http://localhost:8090/TestService.asmx
并且在使用浏览器时它工作正常。
我接到了之前正常工作的 ajax 电话。我只需要更改服务的url:
/TestService.asmx
现在使用 ajax 调用服务时会产生错误:
System.InvalidOperationException: Request format is invalid: application/json; charset=utf-8.
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
我有
contentType: "application/json; charset=utf-8",
在jQueryAjax调用。在沮丧地度过了几个小时之后,我在这个页面上找到了一个 link,它实际上解决了这个问题:
http://www.springframework.net/doc-latest/reference/html/webservices.html
我不想为此任务添加对 spring.net 的额外依赖,因为我已经在使用温莎城堡了。
如何访问在 class 库项目中创建并在 web.config 中注册的 Web 服务,作为 ASP.net 网页?
我在回答我自己的问题,这样遇到这个问题的人就不用再撞墙了。似乎在 class 库项目 (.net Framework 4.5.1) 上创建的 WebService 会 忽略 参数:
ResponseFormat = ResponseFormat.Json
即使您指定 JSON 作为响应格式,它仍然会以 XML 对象响应。
所以,我改变了我的要求来解决这个问题。
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public string Hello(string world)
{
return "Hello " + world;
}
在 javascript 部分
$.post(url, function (result) {
//result is an XML object
});
我首先在 ASP.net 项目中创建了一个 Web 服务,然后将其代码移动到一个 class 库到一个 CSharp(.cs) 文件中。
我还在这个新创建的 class 中添加了 "IHttpHandlerFactory" 的实现,以便我可以在 web.config 文件上注册此服务:
public class Test: WebService, IHttpHandlerFactory
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Hello(string world)
{
return "Hello " + world;
}
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)
{
}
}
并在web.config中注册:
<add name="TestService" path="TestService.asmx" verb="*" type="MyApp.Library.Test, MyApp.Library, Version=1.0.0.0, Culture=neutral" preCondition="integratedMode" />
我可以在这里访问它:
http://localhost:8090/TestService.asmx
并且在使用浏览器时它工作正常。
我接到了之前正常工作的 ajax 电话。我只需要更改服务的url:
/TestService.asmx
现在使用 ajax 调用服务时会产生错误:
System.InvalidOperationException: Request format is invalid: application/json; charset=utf-8.
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
我有
contentType: "application/json; charset=utf-8",
在jQueryAjax调用。在沮丧地度过了几个小时之后,我在这个页面上找到了一个 link,它实际上解决了这个问题:
http://www.springframework.net/doc-latest/reference/html/webservices.html
我不想为此任务添加对 spring.net 的额外依赖,因为我已经在使用温莎城堡了。
如何访问在 class 库项目中创建并在 web.config 中注册的 Web 服务,作为 ASP.net 网页?
我在回答我自己的问题,这样遇到这个问题的人就不用再撞墙了。似乎在 class 库项目 (.net Framework 4.5.1) 上创建的 WebService 会 忽略 参数:
ResponseFormat = ResponseFormat.Json
即使您指定 JSON 作为响应格式,它仍然会以 XML 对象响应。
所以,我改变了我的要求来解决这个问题。
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public string Hello(string world)
{
return "Hello " + world;
}
在 javascript 部分
$.post(url, function (result) {
//result is an XML object
});