WCF服务(NetTcpBinding)+托管WPF应用程序上的主机

时间:2019-07-17 14:40:40

标签: c# wpf wcf

我想用WCF服务创建一个WPF应用程序以侦听某些端口。我创建C#CLass库(UfebsSignServiceLibrary)并添加到WCF服务中。在库中之后,我添加了接口和类实现程序。 我创建了WPF应用程序,并希望在其中托管我的WCF服务。我在WPF APP(配置app.config)中添加了对UfebsSignServiceLibrary的引用。 WPF已启动,但是当我尝试在WCF服务上添加服务引用时,出现错误-元数据包含无法解析的引用。

我已经尝试启用“ httpGetEnabled”,“ httpsGetEnabled”,并为http协议()添加了baseAddress。我读过许多关于同一问题的主题,但没有找到适合我的情况的答案。

我的带有WCF服务代码的类库:

namespace UfebsSignServiceLibrary
    {
        [ServiceContract]
        public interface IUfebsSignService
        {
            [OperationContract]
            byte[] SignData(byte[] data, string profileName);
        }

namespace UfebsSignServiceLibrary
{
    public class UfebsSignService : IUfebsSignService
    {
        public byte[] SignData(byte[] data, string profileName)
        {
            // Some Code
        }
    }

我的WPF APP代码:

public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
                using (host = new ServiceHost(typeof(UfebsSignService)))
                {
                    host.Open();
                }

                Stop.IsEnabled = true;
                Start.IsEnabled = false; 
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            host.Close();
            Stop.IsEnabled = false;
            Start.IsEnabled = true;
        }

App.config代码(wpf应用):

<system.serviceModel>

    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehavior">
          <serviceMetadata  /> <!--httpGetEnabled="false"-->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="mexBehavior" name="UfebsSignServiceLibrary.UfebsSignService">
        <host>
          <baseAddresses>
            <!--<add baseAddress="http://localhost:8080/UfebsSignService" />-->
            <add baseAddress="net.tcp://localhost:8585/UfebsSignService/" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="netTcpBinding" contract="UfebsSignServiceLibrary.IUfebsSignService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />   
      </service>
    </services>
  </system.serviceModel>
  

无法识别URI前缀。   元数据包含无法解析的引用:“ net.tcp:// localhost:8585 / UfebsSignService /”。   无法连接到net.tcp:// localhost:8585 / UfebsSignService /。连接尝试持续时间为00:00:02.0140574。 TCP错误代码10061

1 个答案:

答案 0 :(得分:1)

代码段中有一个小问题。当我们使用USING语句时,它将自动释放托管资源。请考虑使用以下代码启动服务主机。

host = new ServiceHost(service);
host.Open();

请随时让我知道问题是否仍然存在。

相关问题