我按照这个示例制作了我的第一个WCF服务:WCF REST Service JSON,然后编辑它以创建我自己的服务。我能够使用ASP.NET Developer Server使我的服务在调试中运行良好。我打开端口8081(在服务器,防火墙和路由器上),然后将其分配到IIS中的“注册”文件夹。然后我花了一天的时间将服务发布到运行IIS 6的Windows Server 2003,我必须使用MsDeployAgentService。然后,我使用IIS将ASP.NET版本设置为4.0.3019。 Registration文件夹包含Service1.svc,Web.config和bin文件夹。
这是我的网络配置文件:
<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="ServiceApp.Properties.Settings.RegistrationConnection"
connectionString="Data Source=EDITED;Initial Catalog=Registration;Persist Security Info=True;User ID=EDITED;Password=EDITED;MultipleActiveResultSets=True" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpRuntime maxRequestLength="524288" />
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="StreamedRequestWebBinding"
bypassProxyOnLocal="true"
useDefaultWebProxy="false"
hostNameComparisonMode="WeakWildcard"
sendTimeout="10:15:00"
openTimeout="10:15:00"
receiveTimeout="10:15:00"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
transferMode="StreamedRequest">
<readerQuotas maxArrayLength="2147483647"
maxStringContentLength="2147483647" />
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="ServiceApp.Service1" behaviorConfiguration="ServiceBehaviour">
<endpoint address="" binding="webHttpBinding" bindingConfiguration="StreamedRequestWebBinding" contract="ServiceApp.IService1" behaviorConfiguration="web">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
当我尝试使用测试客户端使用服务时,我会使用以下语句获得错误404
byte[] data = client.DownloadData("http://ServerIP:8081/Registration/Service1.svc/CheckSoftware/1/Customer1/1_0/abc123/1/1");
当我删除端口时,我收到错误401“未授权”
byte[] data = client.DownloadData("http://ServerIP/Registration/Service1.svc/CheckSoftware/1/Customer1/1_0/abc123/1/1");
我尝试过使用LAN IP以及WAN IP地址。
关注this post,因为它看起来很喜欢OP遵循相同的例子并且有simalar问题,但它没有帮助。
然后我查看了服务器上的事件查看器,并在我输入端口号时找到了这个:
WebHost failed to process a request.
Sender Information: System.ServiceModel.Activation.HostedHttpRequestAsyncResult/23878916
Exception: System.Web.HttpException (0x80004005): The service '/Registration/Service1.svc' does not exist. ---> System.ServiceModel.EndpointNotFoundException: The service '/Registration/Service1.svc' does not exist.
at System.ServiceModel.ServiceHostingEnvironment.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath)
at System.ServiceModel.ServiceHostingEnvironment.EnsureServiceAvailableFast(String relativeVirtualPath)
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result)
Process Name: w3wp
Process ID: 3460
我可以清楚地看到该文件夹中存在Service1.svc文件。我在这一点上很迷茫,并希望得到任何帮助。
已编辑包含更多代码:
namespace ServiceApp
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET",
UriTemplate = "/CheckSoftware/{SoftwareId}/{CustomerId}/{Version}/{Key}/{MoboID}/{ProcessorID}",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
SoftwareStatus CheckSoftware(string SoftwareID, string Version, string CustomerID, string Key, string moboID, string processorID);
[OperationContract]
[WebInvoke(
Method = "GET",
UriTemplate = "/SaveKey/{SoftwareId}/{CustomerId}/{Key}/{MoboID}/{ProcessorID}",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string SaveKey(string SoftwareID, string CustomerID, string Key, string moboID, string processorID);
}
[DataContract]
public class FileInformation
{
[DataMember]
public string Name
{
get;
set;
}
[DataMember]
public byte[] Content
{
get;
set;
}
}
[DataContract]
public class Employee
{
[DataMember]
public int Id
{
get;
set;
}
[DataMember]
public string Name
{
get;
set;
}
}
[DataContract]
public class SoftwareKey
{
[DataMember]
public string SoftwareId
{
get;
set;
}
[DataMember]
public string CustomerId
{
get;
set;
}
[DataMember]
public string Version
{
get;
set;
}
[DataMember]
public string Key
{
get;
set;
}
}
[DataContract]
public class SoftwareStatus
{
[DataMember]
public Boolean SoftwareId
{
get;
set;
}
[DataMember]
public Boolean CustomerId
{
get;
set;
}
[DataMember]
public Boolean Version
{
get;
set;
}
[DataMember]
public Boolean Key
{
get;
set;
}
[DataMember]
public Boolean HardwareID
{
get;
set;
}
}
}
答案 0 :(得分:0)
您的UriTemplate设置正确吗? 404错误表明它根本没有与服务/方法联系。
REST服务是简单的web apis,而不是像普通的WCF服务那样尝试导入服务,尝试正常的http请求(只是一个简单的例子,而不是解决方案):
//declare the request (not sure of your uri, so i guessed)
var queryString = string.Format("1/{0}/1_0/{1}/1/1", Param1, Param2);
var req =
HttpWebRequest)WebRequest.Create("http://ServerIP:8081/Registration/Service1.svc/CheckSoftware/"); //base address of service
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
//build the fields stream
var fields = Encoding.UTF8.GetBytes(queryString);
req.ContentLength = fields.Length;
//put the request info into the stream
var requestStream = req.GetRequestStream();
requestStream.Write(fields, 0, fields.Length);
requestStream.Close();
//execute the request
var resp = (HttpWebResponse)req.GetResponse();
//pull your infos
var statusCode = (int)resp.StatusCode;
var body = new StreamReader(resp.GetResponseStream()).ReadToEnd();
答案 1 :(得分:0)
将IIS 6.0用于服务时,我不需要在URL中包含文件夹名称。因此,只需更改一行代码即可解决我的问题。
byte[] data = client.DownloadData("http://ServerIP:8081/Service1.svc/CheckSoftware/1/Customer1/1_0/abc123/1/1");