我有一个asmx服务,它接受一个int参数.我可以打开服务的URL并查看服务描述屏幕.从这里我可以将查询参数输入到表单中并调用Web服务.
有没有办法直接从URL /查询字符串调用Web服务?
这不起作用:
HTTP://本地主机:4653/MyService.asmx OP = MyWebMethod&intParameter = 1
有任何想法吗?由于某些部署问题,我真的希望能够通过标准链接执行此操作.我是否必须在普通的aspx页面中包装请求?
private static string WebServiceCall(string methodName)
{
WebRequest webRequest = WebRequest.Create("http://localhost/AccountSvc/DataInquiry.asmx");
HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
httpRequest.Method = "POST";
httpRequest.ContentType = "text/xml; charset=utf-8";
httpRequest.Headers.Add("SOAPAction: http://tempuri.org/" + methodName);
httpRequest.ProtocolVersion = HttpVersion.Version11;
httpRequest.Credentials = CredentialCache.DefaultCredentials;
Stream requestStream = httpRequest.GetRequestStream();
//Create Stream and Complete Request
StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
StringBuilder soapRequest = new StringBuilder("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
soapRequest.Append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
soapRequest.Append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>");
soapRequest.Append("<GetMyName xmlns=\"http://tempuri.org/\"><name>Sam</name></GetMyName>");
soapRequest.Append("</soap:Body></soap:Envelope>");
streamWriter.Write(soapRequest.ToString());
streamWriter.Close();
//Get the Response
HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
StreamReader srd = new StreamReader(wr.GetResponseStream());
string resulXmlFromWebService = srd.ReadToEnd();
return resulXmlFromWebService;
} …Run Code Online (Sandbox Code Playgroud) 我有一个ASP.net Web服务,我用于Web应用程序,它向我返回XML或JSON数据,具体取决于我调用的函数.到目前为止,这一直运作良好,但我遇到了一个问题.我想在我的页面上创建一个"导出"链接,用于下载JSON文件.链接的格式非常简单:
<a href="mywebserviceaddress/ExportFunc?itemId=2">Export This Item</a>
Run Code Online (Sandbox Code Playgroud)
正如您可能想象的那样,这应该导出第2项.到目前为止,这么好,是吗?
问题是因为我并没有特别要求接受的内容类型是JSON,所以ASP.net绝对拒绝发回除XML以外的任何内容,这对于这种情况并不合适.代码基本如下:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Item ExportItem(int itemId)
{
Context.Response.AddHeader("content-disposition", "attachment; filename=export.json"); //Makes it a download
return GetExportItem(itemId);
}
Run Code Online (Sandbox Code Playgroud)
尽管我将ResponseFormat指定为JSON,但除非我通过AJAX(使用Google Web Toolkit,BTW)请求此方法,否则我总是返回XML:
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, "mywebserviceaddress/ExportFunc");
builder.setHeader("Content-type","application/json; charset=utf-8");
builder.setHeader("Accepts","application/json");
builder.sendRequest("{\"itemId\":2}", new RequestCallback(){...});
Run Code Online (Sandbox Code Playgroud)
这很好,但AJAX不会给我一个下载对话框.有没有办法迫使ASP.net给我回JSON,无论数据是如何请求的?在我看来,没有手动覆盖这种行为是一个严重的设计疏忽.
快速回答:
首先,我想说,我认为womp的答案可能是长期更好的方式(转换为WCF),但是deostroll让我得到了我将在不久的将来使用的答案.此外,应该注意的是,这似乎主要是因为我只想下载,在所有情况下都可能无法正常工作.在任何情况下,这里是我最终用来获得我想要的结果的代码:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void ExportItem(int itemId)
{
Item item = GetExportItem(itemId);
JavaScriptSerializer js = new JavaScriptSerializer();
string str = js.Serialize(item);
Context.Response.Clear();
Context.Response.ContentType = "application/json";
Context.Response.AddHeader("content-disposition", "attachment; filename=export.json");
Context.Response.AddHeader("content-length", str.Length.ToString());
Context.Response.Flush();
Context.Response.Write(str);
}
Run Code Online (Sandbox Code Playgroud)
请注意 …
我有2个Web服务,共有大约6个Web方法,大多数代码都是以任何方式放在程序集中,而web服务asmx实际上只是调用这些程序集方法并返回它们的返回类型.
将Web服务从ASMX转换为WCF要付出多少努力?
我几乎在这个阶段控制唯一 - 连接到Web服务的非基于Web的客户端,所以这不是一个真正的问题,产品是在预发布.
我有一个Web应用程序,为JSON和ASMX Web服务提供WCF REST API.该应用程序已存在几年.它基于ASP.NET 2.0,但几年前升级到.NET 4.0,我刚刚升级到.NET 4.5以便能够使用新的异步框架.
应用程序的背后是一些遗留服务,我意识到通过异步提高性能有很大的潜力.我已经在应用程序中实现了异步,并且一切都通过WCF REST API完美地运行.
太晚了我发现ASMX API失败了,我想要这样的方法:
[WebMethod(Description = "Takes an internal trip ID as parameter.")]
async public Task<Trip> GetTrip(int tripid)
{
var t = await Trip.GetTrip(tripid);
return t;
}
Run Code Online (Sandbox Code Playgroud)
然后我了解到ASMX根本不支持async/await,并且每个人都建议迁移到WCF.我对此并不太满意.ASMX(实际上是其中三个)填充了不同的方法,并且我们希望从旧的API继续提供大量的API使用者.
但我们需要提高性能!有没有人知道一个解决方法,所以我可以继续在ASMX后面使用async/await,但是像以前一样公开ASMX?
我在ASP.NET 4.0中有一个Web应用程序.我添加了一个asmx服务,主要是作为自动完成扩展器的查找值的源.
当我在本地机器上调试时,一切正常.但是,当我将Web应用程序部署到IIS 7.5时,我在尝试将数据发送到服务时收到HTTP 404响应.
我能够浏览到服务定义,查看可用的操作.然而,引人注目的是,当我使用测试页面使用POST测试服务时,我再次收到HTTP 404.
我不确定发生了什么.我确实在我的Web应用程序中创建了asmx文件,它部署在我正在运行的生产应用程序的虚拟目录中.
是否存在将.asmx文件部署在同一虚拟目录中的问题?
所以我在尝试让我的asmx webservice使用依赖注入和使用IoC来实现时遇到了困难.我希望我的webservice能够使用我的内部业务层服务.Web服务将由来自不同域的外部客户端使用,主要用于发送和接收有关订单和客户等实体的信息.
一个例子是:
public class MyService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return new MyBusinessService().MyMethod();
}
}
public class MyBusinessService : IMyBusinessService
{
public string MyMethod()
{
return "hello";
}
}
Run Code Online (Sandbox Code Playgroud)
我想使用依赖注入来消除"新"我的服务的需要,但我无法找到一种方法来做到这一点.我可以使用穷人的DI来使用它,或者至少我认为它被称为"穷人".
像这样:
public class MyService : System.Web.Services.WebService
{
private IMyBusinessService _myService;
public MyService(IMyBusinessService myService)
{
_myService = myService;
}
public MyService() : this(new MyBusinessServie()) { }
[WebMethod]
public string HelloWorld()
{
return _myService.MyMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
但我简直无法理解如何使用IoC容器来注入我的依赖项,因为我无法让服务在没有无参数构造函数的情况下运行.请善待,我不是一个有经验的程序员和刚开始测试的依赖注入,并得到它的工作在我的Windows罚款structuremap形成的应用,但就死在这一个.
我试图从jQuery调用ASMX方法但没有成功.以下是我的代码,我不明白我错过了什么.
File Something.js,
function setQuestion() {
$.ajax({
type: "POST",
data: "{}",
dataType: "json",
url: "http: //localhost/BoATransformation/Survey.asmx/GetSurvey",
contentType: "application/json; charset=utf-8",
success: onSuccess
});
}
function onSuccess(msg) {
$("#questionCxt").append(msg);
}
Run Code Online (Sandbox Code Playgroud)
File SomethingElse.cs,
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class Survey : System.Web.Services.WebService {
public Survey () {
}
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string GetSurvey() {
return "Question: Who is Snoopy?";
}
}
Run Code Online (Sandbox Code Playgroud) 如何使用javascript(aspx)....从客户端调用服务器端(aspx.cs)中的非静态方法....?
据我所知,我可以从客户端调用服务器端的静态方法...
服务器端:
[WebMethod]
public static void method1()
{
}
Run Code Online (Sandbox Code Playgroud)
客户端:
<script language="JavaScript">
function keyUP()
{
PageMethods.method1();
}
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
Run Code Online (Sandbox Code Playgroud)
有用.现在如何从客户端调用非静态方法?
(之前已经提出了类似问题的问题,但问题和接受的答案都没有提供我正在寻找的细节)
为了在专用域帐户下运行asmx Web服务,使用具有域帐户身份和模拟身份的应用程序池的使用方案和/或优缺点是什么?
我们有3个小型内部Web服务,它们在相对较低的负载下运行,我们希望将它们切换到在自己的域帐户下运行(为了与SQL Server等集成安全性).我似乎可以选择为每个应用程序创建专用的应用程序池,或者为所有应用程序创建一个应用程序池,并在每个应用程序中使用模拟.
我理解应用程序池提供工作进程隔离,并且在使用模拟时需要考虑性能,但是除了那些除了正确的选项之外还有什么呢?
asmx ×10
c# ×5
asp.net ×3
web-services ×3
wcf ×2
asynchronous ×1
dependencies ×1
iis ×1
iis-7 ×1
jquery ×1
json ×1
soap ×1
webmethod ×1