我正在尝试托管此服务,但是当我在另一个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服务时,我正在运行该服务。
答案 0 :(得分:3)
在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();
}
}
}
答案 1 :(得分:1)
您需要使用有关服务的信息创建/更新app.config文件。 查看:http://msdn.microsoft.com/en-us/library/ms734765.aspx