我有控制台应用程序作为主机和wcf库项目。当我想将应用程序托管到http://localhost:59350/GettingStarted/CalculatorService
时,它表示无法找到该页面。此代码来自此处的msdn教程http://msdn.microsoft.com/en-us/library/ms730935.aspx。
以下是代码:
using GettingStartedLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
namespace GettingStartedHost
{
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:59350/GettingStarted");
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
try
{
selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate the service.");
Console.WriteLine();
Console.ReadLine();
selfHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occured: {0}", ce.Message);
selfHost.Abort();
}
}
}
}
ICalculator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace GettingStartedLib
{
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Devide(double n1, double n2);
}
}
CalculatorService的
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace GettingStartedLib
{
public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
{
double result = n1 + n2;
Console.WriteLine("Recieved Add {0},{1}", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Subtract(double n1, double n2)
{
double result = n1 - n2;
Console.WriteLine("Recieved Subtract {0},{1}", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Multiply(double n1, double n2)
{
double result = n1 * n2;
Console.WriteLine("Recieved Multiply {0},{1}", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
public double Devide(double n1, double n2)
{
double result = n1 / n2;
Console.WriteLine("Recieved Devide {0},{1}", n1, n2);
Console.WriteLine("Return: {0}", result);
return result;
}
}
}
答案 0 :(得分:0)
您无法使用浏览器访问您的服务,因为您使用自托管。(与Web服务不同)
您可以使用wcftestclient来测试wcf服务,Read this article
或者您可以在VS中添加服务引用以使用服务。