简体   繁体   English

测试ASMX webservice

[英]Test ASMX webservice

I wrote a simple webservice for uploading a file. 我写了一个简单的web服务来上传文件。

<%@ WebService Language="C#" class="AppWebService" %>

using System;
using System.Web.Services;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.IO;


[WebService(Namespace="http://myip/services")]
public class AppWebService : WebService
{    
    [WebMethod]
    public string UploadFile(byte[] f, string fileName)
    {
        // the byte array argument contains the content of the file
        // the string argument contains the name and extension
        // of the file passed in the byte array
        try
        {
            // instance a memory stream and pass the
            // byte array to its constructor
            MemoryStream ms = new MemoryStream(f);

            // instance a filestream pointing to the 
            // storage folder, use the original file name
            // to name the resulting file
            FileStream fs = new FileStream
                (System.Web.Hosting.HostingEnvironment.MapPath("/TransientStorage/") +
                fileName, FileMode.Create);

            // write the memory stream containing the original
            // file as a byte array to the filestream
            ms.WriteTo(fs);

            // clean up
            ms.Close();
            fs.Close();
            fs.Dispose();

            // return OK if we made it this far
            return "OK";
        }
        catch (Exception ex)
        {
            // return the error message if the operation fails
            return ex.Message.ToString();
        }
    }




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

}

Now I am trying to test the functionality but am having trouble interacting with the webservice via C#. 现在我正在尝试测试功能,但是无法通过C#与Web服务进行交互。 I've searched around tried using HTTPWebrequest (multipart/form-data) that I found in this post but didn't have much success and am not sure that this is the right approach. 我在周围搜索过尝试使用我在这篇文章中找到的HTTPWebrequest (multipart/form-data) ,但没有取得多大成功,我不确定这是正确的方法。

How can I test the webservice that I wrote to see if I can successfully upload a file? 如何测试我写的Web服务以查看是否可以成功上传文件?

Are you looking to write test cases or just run some tests via curl or a ui 您是要编写测试用例还是仅通过curl或ui运行一些测试

Could use WCF Test Client Could use curl 可以使用WCF测试客户端可以使用curl

Here is a link to some code which should help as well. 这是一些代码的链接 ,这些代码也应该有所帮助。

One easy to test the code is to create unit test by right clicking on method you want to test and select Create Unit Tests. 一个易于测试代码的方法是通过右键单击要测试的方法创建单元测试,然后选择Create Unit Tests。 You will have a test method stub generated for you with all required variables initialized to null. 您将为您生成一个测试方法存根,并将所有必需变量初始化为null。 Initialize all the variables with the data you want and run the unit tests. 使用所需数据初始化所有变量并运行单元测试。 This will test the method itself. 这将测试方法本身。
I am not sure if this is what you are looking for. 我不确定这是不是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM