尝试按照此示例使其正常工作:http://weblogs.asp.net/kiyoshi/archive/2008/10/08/wcf-using-webhttpbinding-for-rest-services.aspx
这是我的App.config
:
<system.serviceModel>
<services>
<!-- The service for the TEST WEB client -->
<service name="MyServer.AAServiceType" behaviorConfiguration="Default">
<endpoint address="testservice"
binding="webHttpBinding" behaviorConfiguration="webBehavior"
contract="MyServer.AAIContractName" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8787/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<!-- TEST WEB BEHAVIOR -->
<behavior name="Default">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
<!-- TEST WEB ENDPOINT -->
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
更新:服务合同是:
namespace MyServer
{
[ServiceContract(SessionMode=SessionMode.NotAllowed)]
public interface IContractName
{
[WebGet(UriTemplate = "date/{year}/{month}/{day}", ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
string GetDate(string day, string month, string year);
}
public class ServiceType : IContractName
{
public string GetDate(string day, string month, string year)
{
return new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day)).ToString("dddd, MMMM dd, yyyy");
}
}
}
问题在于,当我尝试连接到8787端口时(例如,使用putty
),将返回“连接被拒绝”错误。正如您所看到的,我还尝试在合同类和服务实现中添加错误的名称,并且没有例外。我做错了什么,拜托?
答案 0 :(得分:1)
您是在IIS中托管还是自托管?
如果你在IIS(使用* .svc文件)中托管它,那么IIS会指示地址 - 它将是
http://yourserver/yourvirtualdirectory/yourservice.svc/.........
如果你自我主持,那么一切似乎都对我好 - 在这种情况下,你的基地发挥作用:
http://localhost:8787/testservice
现在应该是您的服务地址。
答案 1 :(得分:1)
启动该服务的代码出错。正确的代码是:
using (ServiceHost serviceHost = new ServiceHost(typeof(ServiceType)))
{
try
{
// Open the ServiceHost to start listening for messages.
serviceHost.Open();
// The service can now be accessed.
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.ReadLine();
// Close the ServiceHost.
serviceHost.Close();
}
catch (CommunicationException commProblem)
{
Console.WriteLine(commProblem.Message);
Console.ReadLine();
}
}