如何在 Visual Studio 中创建不使用 Web 服务器的 WCF 服务应用程序
How can I create a WCF Service Application in Visual Studio that does NOT use a Web Server
我有一个简单的任务:一个程序(可执行)应该使用一些参数调用另一个程序(也是可执行)的函数。应该启动程序 A,调用该函数,然后终止。程序 B 是遗留程序,具有 GUI 和 运行s 连续。这两个程序 运行 在同一台 Windows PC 上使用 .NET Framework。我没有网络开发经验,程序 B 不应该 运行 作为网络服务!命名管道似乎是个不错的选择。
我研究了最好的方法是什么,并想尝试 WCF。该文档声称 "A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application"。据我所知,我可以 运行 程序 B 作为服务而无需托管 Web 服务器。
然而,我在 Visual Studio 中看到的一切似乎都假定我想要 运行 一个服务器。 Wenn 我想在 Visual Studio 中创建一个新的 WCF 项目,唯一的选择是库或 "A project for creating WCF service application that is hosted in IIS/WAS"。一旦我创建了上述项目,调试器就希望我选择一个浏览器来托管服务。
在另一个 Whosebug 主题中,一个流行的建议是使用 this 网站作为指南并简单地删除 http 引用,因为该指南同时适用于命名管道和 http。另一个迹象表明它应该是可能的。
有人能给我指出正确的方向吗?我错过了什么?如何在不涉及 Web 开发的情况下使用 WCF?
你已经在路上了,在程序B中托管web服务就可以了,不用指定web服务器。这称为自托管 WCF。正如您提供的link提到的,Service host class用于承载WCF服务,这意味着我们可以在Console/Winform
中承载该服务,依此类推。
这是在 Winform 应用程序中托管服务的示例。
public partial class Form1 : Form
{
ServiceHost serviceHost = null;
public Form1()
{
InitializeComponent();
Uri uri = new Uri("http://localhost:9009");
BasicHttpBinding binding = new BasicHttpBinding();
serviceHost = new ServiceHost(typeof(MyService), uri);
serviceHost.AddServiceEndpoint(typeof(IService), binding, "");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior()
{
HttpGetEnabled = true
};
serviceHost.Description.Behaviors.Add(smb);
System.ServiceModel.Channels.Binding mexbinding = MetadataExchangeBindings.CreateMexHttpBinding();
serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");
serviceHost.Open();
}
private void Form1_Load(object sender, EventArgs e)
{
if (serviceHost.State==CommunicationState.Opened)
{
this.label1.Text = "Service is running";
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serviceHost.State==CommunicationState.Opened&&serviceHost.State!=CommunicationState.Closed)
{
serviceHost.Close();
}
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
string Test();
}
public class MyService:IService
{
public string Test()
{
return DateTime.Now.ToLongTimeString();
}
}
之后,我们可以使用客户端代理来使用它。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
如果有什么我可以帮忙的,请随时告诉我。
我有一个简单的任务:一个程序(可执行)应该使用一些参数调用另一个程序(也是可执行)的函数。应该启动程序 A,调用该函数,然后终止。程序 B 是遗留程序,具有 GUI 和 运行s 连续。这两个程序 运行 在同一台 Windows PC 上使用 .NET Framework。我没有网络开发经验,程序 B 不应该 运行 作为网络服务!命名管道似乎是个不错的选择。
我研究了最好的方法是什么,并想尝试 WCF。该文档声称 "A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application"。据我所知,我可以 运行 程序 B 作为服务而无需托管 Web 服务器。
然而,我在 Visual Studio 中看到的一切似乎都假定我想要 运行 一个服务器。 Wenn 我想在 Visual Studio 中创建一个新的 WCF 项目,唯一的选择是库或 "A project for creating WCF service application that is hosted in IIS/WAS"。一旦我创建了上述项目,调试器就希望我选择一个浏览器来托管服务。
在另一个 Whosebug 主题中,一个流行的建议是使用 this 网站作为指南并简单地删除 http 引用,因为该指南同时适用于命名管道和 http。另一个迹象表明它应该是可能的。
有人能给我指出正确的方向吗?我错过了什么?如何在不涉及 Web 开发的情况下使用 WCF?
你已经在路上了,在程序B中托管web服务就可以了,不用指定web服务器。这称为自托管 WCF。正如您提供的link提到的,Service host class用于承载WCF服务,这意味着我们可以在Console/Winform
中承载该服务,依此类推。
这是在 Winform 应用程序中托管服务的示例。
public partial class Form1 : Form
{
ServiceHost serviceHost = null;
public Form1()
{
InitializeComponent();
Uri uri = new Uri("http://localhost:9009");
BasicHttpBinding binding = new BasicHttpBinding();
serviceHost = new ServiceHost(typeof(MyService), uri);
serviceHost.AddServiceEndpoint(typeof(IService), binding, "");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior()
{
HttpGetEnabled = true
};
serviceHost.Description.Behaviors.Add(smb);
System.ServiceModel.Channels.Binding mexbinding = MetadataExchangeBindings.CreateMexHttpBinding();
serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");
serviceHost.Open();
}
private void Form1_Load(object sender, EventArgs e)
{
if (serviceHost.State==CommunicationState.Opened)
{
this.label1.Text = "Service is running";
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (serviceHost.State==CommunicationState.Opened&&serviceHost.State!=CommunicationState.Closed)
{
serviceHost.Close();
}
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
string Test();
}
public class MyService:IService
{
public string Test()
{
return DateTime.Now.ToLongTimeString();
}
}
之后,我们可以使用客户端代理来使用它。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
如果有什么我可以帮忙的,请随时告诉我。