gpt4 book ai didi

c# - 未检测到 Web 服务?

转载 作者:太空狗 更新时间:2023-10-30 01:24:17 24 4
gpt4 key购买 nike

我正在尝试在下面托管这项运行良好的服务,但是当我在不同的 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 或应用程序配置文件,它只是从控制台应用程序托管。

另请注意,当我尝试添加 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

关于c# - 未检测到 Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9892070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com