using System;
using System.ServiceModel;
// 定义一个服务契约
[ServiceContract]
public interface IMyService
{
[OperationContract]
string GetData(int value);
}
// 实现服务契约
public class MyService : IMyService
{
public string GetData(int value)
{
return $"You entered: {value}";
}
}
// 调用WebService的客户端代码
class Program
{
static void Main(string[] args)
{
// 创建一个绑定和终结点地址
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://example.com/MyService.svc");
// 创建通道工厂并打开
ChannelFactory<IMyService> factory = new ChannelFactory<IMyService>(binding, address);
IMyService proxy = factory.CreateChannel();
try
{
// 调用WebService方法
string result = proxy.GetData(42);
Console.WriteLine(result);
}
finally
{
// 关闭通道和工厂
((IClientChannel)proxy).Close();
factory.Close();
}
}
}
ServiceContract 和 OperationContract 属性来定义服务接口 IMyService,其中包含一个名为 GetData 的操作方法。MyService 来实现 IMyService 接口,并在其中实现 GetData 方法。BasicHttpBinding 和 EndpointAddress 来指定 WebService 的绑定和地址。ChannelFactory<T> 来创建一个通道工厂,并通过它创建一个代理对象 proxy。GetData。这段代码展示了如何在 C# 中调用一个简单的 WebService。
上一篇:c# checkbox
下一篇:c#格式化字符串
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站