C#/.NET 分布式专题(SOA【面向服务】WebService的使用)

对于分布式框架,微服务专题后续会介绍到。分布式框架主要有(zookeeper,dubbo,ocelot/consul)

WebService:寄宿在IIS,也就是必须在网站项目
Http协议 SOAP协议
1 Http传输信道,A服务器到B服务器,数据是什么格式传递的
2 XML的数据格式—Http传输解析得到的有用数据
3 SOAP协议—封装格式:在分布式的环境中,描述了如何做数据交换的一个轻量级协议
4 WSDL:属于webservice的标配,标准化描述服务,方便调用
5 UDDI:根据描述查找服务的机制
服务端调用WebService添加服务引用,基于svcUtil.exe生成的
基于wsdl生成的一个代理:屏蔽服务调用的复杂性
单元测试:测试方法—回归测试
WebService安全认证:
Form认证 windows认证
服务方法里面添加账号密码参数
SoapHeader验证

下面来说说WebService的使用:
一:在asp.net网站的路由中配置如下代码

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("Remote/{*pathInfo}");//忽略以Remote开头

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

二:服务端

/// 
/// MyWebService 的摘要说明
/// 
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
// [System.Web.Script.Services.ScriptService]

public class MyWebService : System.Web.Services.WebService
{
    public CustomSoapHeader SoapHeaderProp { get; set; }

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

    [WebMethod]
    public string HelloWorldWithAuth(string name_password)
    {
        if (true) { }
        return "Hello World";
    }
    [WebMethod]
    public string GetName(int i)
    {
        return $"{i}加菲猫";
    }
    [WebMethod]//webservice的方法就没有重载
    public int Plus(int x, int y)
    {
        return x + y;
    }
    [WebMethod]
    [SoapHeader("SoapHeaderProp")]
    public string GetInfo(int id, string name)
    {
        if (!this.SoapHeaderProp.Validate())
        {
            throw new SoapException("身份验证失败", SoapException.ClientFaultCode);
        }
        return Newtonsoft.Json.JsonConvert.SerializeObject(new
        {
            Id = id,
            Name = name,
            Remark = $"This is {id} {name}"
        });
    }

    [WebMethod]
    [SoapHeader("SoapHeaderProp")]
    public UserInfo GetUser(int id)
    {
        if (!this.SoapHeaderProp.Validate())
        {
            throw new SoapException("身份验证失败", SoapException.ClientFaultCode);
        }
        return new UserInfo()
        {
            Id = id,
            Name = "楠nan",
            Age = 27
        };
    }
    [WebMethod]
    [SoapHeader("SoapHeaderProp")]
    public List<UserInfo> GetUserList()
    {
        if (!this.SoapHeaderProp.Validate())
        {
            throw new SoapException("身份验证失败", SoapException.ClientFaultCode);
        }
        return new List<UserInfo>()
        {
            new UserInfo()
            {
                Id = 3,
                Name = "一路有你",
                Age = 27
            },
                new UserInfo()
            {
                Id = 2,
                Name = "感谢有梦",
                Age = 25
            }
        };
    }
}

三:引用端

/// 
/// svcUtil.exe
/// 
[TestClass]
public class UnitTestWebService
{
    static UnitTestWebService()
    {
        Console.WriteLine("完成静态构造函数");
    }

    [TestInitialize]//初始化
    public void Initssa132132434()
    {
        Console.WriteLine("完成初始化过程");
    }

    [TestMethod]
    public void TestMethod()
    {
        using (MyWebServiceTest.MyWebServiceSoapClient client = new MyWebServiceTest.MyWebServiceSoapClient())
        {
            MyWebServiceTest.CustomSoapHeader header = new MyWebServiceTest.CustomSoapHeader();
            header.UserName = "Eleven";
            header.PassWord = "123456";

            int iResult = client.Plus(12, 33);//45
            MyWebServiceTest.UserInfo userInfo = client.GetUser(header, 1);
            //List userList = client.GetUserList(header);
            var userList = client.GetUserList(header);
        }
    }

    [TestMethod]
    public void TestMethodAssert()
    {
        using (MyWebServiceTest.MyWebServiceSoapClient client = new MyWebServiceTest.MyWebServiceSoapClient())
        {
            Assert.AreEqual(client.Plus(12, 33), 45);
            Assert.AreEqual(client.Plus(12, 44), 56);
            Assert.AreEqual(client.Plus(12, 55), 67);
            Assert.AreEqual(client.Plus(12, 66), 45);
        }
    }
}

你可能感兴趣的:(.Net通用工具)