我想在Windows服务上托管我的WCF Web服务,以便前端可以访问这些方法。
如果我点击VS中的服务(在浏览器中查看),该服务运行正常。但是当我厌倦了使用Windows服务来托管它时,它会返回" Endpoint not found"当我使用基地址和"方法不允许"时从浏览器当我具体说明我想要访问的方法时。
以下是我的代码: 主持人的App.Config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehavior">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
<webHttp defaultOutgoingResponseFormat="Json" />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="mexBehavior" name="BestWebService.Service1">
<host>
<baseAddresses>
<add baseAddress="http://localhost/Service1.svc"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="jsonBehavior"
contract="BestWebService.IService1"
bindingConfiguration="jsonBinding">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="jsonBinding">
<security mode="None">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
&#13;
Service.cs
protected override void OnStart(string[] args)
{
if (host != null) {
host.Close();
}
try
{
host = new ServiceHost(typeof(BestWebService.Service1));
host.Open();
}
catch (Exception e) {
string stored = @"c:\\SETUP ERROR.txt";
using (StreamWriter sw = File.AppendText(stored))
{
sw.WriteLine("1: {0}", e.ToString());
}
host.Close();
}
}
&#13;
WCF服务接口
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat =WebMessageFormat.Json)]
List<WKSTEnviornment> GetWKSTDetails();
}
&#13;
的Javascript
getAllVersions = function (id) {
$.ajax({
url: 'http://localhost/Service1.svc/GetWKSTDetails',
method: 'post',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
var verlist = new Array();
var appname = new Array();
var applications = new Array();
var name = null;
var Object = {};
$.each(data.d, function (i) {
var apps = data.d[i]["AppList"].split('_');
verlist.push(data.d[i]["Version"]);
$.each(apps, function (i) {
appname.push(apps[i]);
})
appname.sort();
var ver = data.d[i]["Version"];
Object[ver] = { Date: data.d[i]["Date"], Applist: appname };
appname = [];
});
versionlist = Object;
getversion = true;
createDeployPanel(Object, verlist, id);
},
error: function (data) {
alert(JSON.stringify(data) + 'Get Versions Failed');
}
});
}
&#13;
谢谢!
答案 0 :(得分:0)
我无法弄清楚Windows服务的问题(服务是成功托管的)所以我把它切换到IIS(配置和CORS也花了我一些时间来解决),现在它正在工作:)