出现此问题的原因是因为ASP.NET Core Web App和Windows Form App使用的.NET版本不同,因此在引用服务时会产生不同的结果。
为了解决这个问题,我们可以使用共享的类库项目作为两个应用程序的中介。在该共享类库中使用服务引用,并在Web App和Windows Form App中引用该类库即可。
以下是一个示例代码:
1.创建共享类库
例如:
public class MyServiceClass
{
private MyServiceClient serviceClient;
public MyServiceClass()
{
serviceClient = new MyServiceClient();
}
public string GetMessage()
{
return serviceClient.GetMessage();
}
}
2.在ASP.NET Core Web App中使用共享类库
例如:
public class HomeController : Controller
{
private MyServiceClass myService;
public HomeController()
{
myService = new MyServiceClass();
}
public IActionResult Index()
{
ViewData["Message"] = myService.GetMessage();
return View();
}
}
3.在Windows Form App中使用共享类库
例如:
public partial class MainForm : Form
{
private MyServiceClass myService;
public MainForm()
{
InitializeComponent();
myService = new MyServiceClass();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(myService.GetMessage());
}
}
上一篇:ASP.NETCoreWebApp的UI部分仅支持HTTPS,但WebApp内部的API调用仅支持HTTP。
下一篇:ASP.NETCoreWebApplicationFactory.CreateClient方法不支持客户端流式传输的操作。