Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在尝试启动第一个WCF服务_C#_.net_Wcf_Service - Fatal编程技术网

C# 正在尝试启动第一个WCF服务

C# 正在尝试启动第一个WCF服务,c#,.net,wcf,service,C#,.net,Wcf,Service,我正在尝试启动我的第一个WCF服务 我想指出,我已经完全理解了WCF体系结构和支柱(ABC:Address Binding and Contract=Endpoint)。此外,我已经理解了WCF哲学的许多元素,所以,我不是一个纯粹的新手 然而,撇开理论不谈,当有人把手放在真实的东西上时,真正的问题就会出现 我有以下三个文件: 文件IService1.cs using System; using System.Collections.Generic; using System.Linq; usin

我正在尝试启动我的第一个WCF服务

我想指出,我已经完全理解了WCF体系结构和支柱(ABC:Address Binding and Contract=Endpoint)。此外,我已经理解了WCF哲学的许多元素,所以,我不是一个纯粹的新手

然而,撇开理论不谈,当有人把手放在真实的东西上时,真正的问题就会出现

我有以下三个文件:

文件
IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

/// <summary>
/// This is the interface that specifies contract for the Sevice1 of this service application.
/// In this file the interface is specified in order to set the service operations that can be invoked by requestors.
/// </summary>
namespace EchoWcfLibrary {
    /// <summary>
    /// The interface specifies for those classes implementing it (services), the operation that the service will expose.
    /// </summary>
    [ServiceContract]
    public interface IService1 {
        // This does not use serialization (implicit serialization in considered: base types used).
        [OperationContract]
        string GetData(int value);
        // This uses data contracts and serialization.
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }

    /// <summary>
    /// The following class defines data contract for those operations managing with non primitive types and, for this reason, needing serialization support (explicit, not implicit)
    /// </summary>
    [DataContract]
    public class CompositeType {
        // Members not serialized
        bool boolValue = true;
        string stringValue = "Hello ";
        // Serialized
        [DataMember]
        public bool BoolValue {
            get { return boolValue; }
            set { boolValue = value; }
        }
        // Serialized
        [DataMember]
        public string StringValue {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

/// <summary>
/// This is the service host implementation. A class implementing the service is specified.
/// </summary>
namespace EchoWcfLibrary {
    /// <summary>
    /// This class implements the IService1 service.
    /// </summary>
    public class Service1 : IService1 {
        // One operation.
        public string GetData(int value) {
            return string.Format("You entered: {0}", value);
        }
        // The other operation.
        public CompositeType GetDataUsingDataContract(CompositeType composite) {
            if (composite == null) {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue) {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using EchoWcfLibrary;

namespace WcfServiceApplication {
    public static class Program {
        static void Main(string[] args) {
            // Setting endpoints and setting the service to start properly.
            // Base address specified: http://localhost:8080/service1
            using (ServiceHost host = new ServiceHost(typeof(Service1), new Uri("http://localhost:8080/service1"))) {
                host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), "svc");
                host.AddServiceEndpoint(typeof(IService1), new NetTcpBinding(), "net.tcp://localhost:8081/service1/tcpsvc");
                host.Open();
                System.Threading.Thread.Sleep(1000000);
                host.Close();
            }
        }
    }
}
这些文件放在名为
EchowFlibrary

和主要的:
Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

/// <summary>
/// This is the interface that specifies contract for the Sevice1 of this service application.
/// In this file the interface is specified in order to set the service operations that can be invoked by requestors.
/// </summary>
namespace EchoWcfLibrary {
    /// <summary>
    /// The interface specifies for those classes implementing it (services), the operation that the service will expose.
    /// </summary>
    [ServiceContract]
    public interface IService1 {
        // This does not use serialization (implicit serialization in considered: base types used).
        [OperationContract]
        string GetData(int value);
        // This uses data contracts and serialization.
        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }

    /// <summary>
    /// The following class defines data contract for those operations managing with non primitive types and, for this reason, needing serialization support (explicit, not implicit)
    /// </summary>
    [DataContract]
    public class CompositeType {
        // Members not serialized
        bool boolValue = true;
        string stringValue = "Hello ";
        // Serialized
        [DataMember]
        public bool BoolValue {
            get { return boolValue; }
            set { boolValue = value; }
        }
        // Serialized
        [DataMember]
        public string StringValue {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

/// <summary>
/// This is the service host implementation. A class implementing the service is specified.
/// </summary>
namespace EchoWcfLibrary {
    /// <summary>
    /// This class implements the IService1 service.
    /// </summary>
    public class Service1 : IService1 {
        // One operation.
        public string GetData(int value) {
            return string.Format("You entered: {0}", value);
        }
        // The other operation.
        public CompositeType GetDataUsingDataContract(CompositeType composite) {
            if (composite == null) {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue) {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using EchoWcfLibrary;

namespace WcfServiceApplication {
    public static class Program {
        static void Main(string[] args) {
            // Setting endpoints and setting the service to start properly.
            // Base address specified: http://localhost:8080/service1
            using (ServiceHost host = new ServiceHost(typeof(Service1), new Uri("http://localhost:8080/service1"))) {
                host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), "svc");
                host.AddServiceEndpoint(typeof(IService1), new NetTcpBinding(), "net.tcp://localhost:8081/service1/tcpsvc");
                host.Open();
                System.Threading.Thread.Sleep(1000000);
                host.Close();
            }
        }
    }
}
最后一个文件位于名为
WcfServiceApplication
这两个项目存在于同一个解决方案中。 当然,
WcfServiceApplication
有到其他项目的链接

我想启动这个服务,正如您所看到的,它是VisualStudio放在WCF库模板中的服务

我第一次尝试运行它时遇到了一些http名称空间保留问题,我使用netsh修复了它,并为我的用户和指定的http名称空间添加了显式保留

然而,我遇到的是:WCF主机应用程序是一个非常有用的小应用程序,它显示了当前托管的服务。只有一个托管服务:我的,但它的状态是停止的,它在描述框中告诉我,没有定义端点

但是我在
Program.cs
中定义了它们。。。我不明白。。。 我做错了什么

谢谢

PS 请注意,即使仅定义
host.AddServiceEndpoint(typeof(IService1)、新的BasicHttpBinding()、“svc”)
也会给出相同的结果


还有一件事:我知道这种构建服务的方法不是很好。。。不过,我想先了解如何从根目录创建和运行服务,然后再了解如何使用更高级别的工具来创建和运行服务,而不是使用自动生成代码的工具。。。谢谢

问题似乎是您定义了两个端点,“svc”(http)和“net”。tcp://localhost:8081/service1/tcpsvc,然后尝试使用第三个端点启动服务主机,该端点未在您配置的两个端点中定义


我建议删除以编程方式创建绑定的代码,将.config文件添加到项目中,然后使用Visual Studio中内置的WCF服务配置编辑器(从2008年起)为您完成繁重的工作。

我理解您的建议,谢谢您,但。。。我真的很讨厌让IDE来做我的工作,或者更好的是,在开始的时候,我想知道如何从地面开始服务。。。。然而。。。第三个端点在哪里??:)感谢您在您的代码中,您要求WCF基于此端点创建服务主机:在服务主机的配置中使用(ServiceHost host=new ServiceHost(typeof(Service1),new Uri(“)),您已经定义了一个http端点,如下所示(没有基本url):)new BasicHttpBinding(),“svc”);希望有意义。