未检测到 Web 服务?

2024-02-08

我正在尝试在下面托管此服务,该服务运行良好,但是当我在不同的 Visual Studio 运行时中打开一个新项目并尝试添加 Web 服务时,它找不到任何东西?不在指定的地址或本地计算机上的任何位置?下面的代码似乎只有当我在同一解决方案中运行它时才有效?

namespace Students
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create the address for the service
            Uri address = new Uri("http://localhost:8001");
            // Create the binding for the service
            WSHttpBinding binding = new WSHttpBinding();
            // Create the service object
            StudentService service = new StudentService();
            // Create the host for the service
            ServiceHost host = new ServiceHost(service, address);
            // Add the endpoint for the service using the contract, binding and name
            host.AddServiceEndpoint(typeof(IStudentService),
                                    binding,
                                    "students");

            // Open the host
            host.Open();
            Console.WriteLine("Student service started");
            Console.WriteLine("Press return to exit");
            Console.ReadLine();
            // Close the host
            host.Close();
        }
    }


}

当我尝试从新项目(与当前解决方案分开)添加它时出现的错误是:

The HTML document does not contain Web service discovery information.
Metadata contains a reference that cannot be resolved: 'http://localhost:8001/'.
There was no endpoint listening at http://localhost:8001/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
The remote server returned an error: (404) Not Found.
If the service is defined in the current solution, try building the solution and adding the service reference again.

当我下载这个案例研究(入门项目)时,它在刚刚从控制台应用程序托管的任何地方都没有网络或应用程序配置文件。

另请注意,当我尝试添加 Web 服务时,该服务正在运行。


在 ServiceHost 中添加元数据交换行为。

namespace Students
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create the address for the service
            Uri address = new Uri("http://localhost:8001");
            // Create the binding for the service
            WSHttpBinding binding = new WSHttpBinding();
            // Create the service object
            StudentService service = new StudentService();
            // Create the host for the service
            ServiceHost host = new ServiceHost(service, address);
            // Add the endpoint for the service using the contract, binding and name
            host.AddServiceEndpoint(typeof(IStudentService),
                                    binding,
                                    "students");

            // Add metadata exchange
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            host.Description.Behaviors.Add(smb);

            // Open the host
            host.Open();
            Console.WriteLine("Student service started");
            Console.WriteLine("Press return to exit");
            Console.ReadLine();
            // Close the host
            host.Close();
        }
    }
}

http://wcftutorial.net/WCF-Self-Hosting.aspx http://wcftutorial.net/WCF-Self-Hosting.aspx

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

未检测到 Web 服务? 的相关文章

随机推荐