0

我按照这个示例制作了我的第一个 WCF 服务:WCF REST Service JSON,然后对其进行了编辑以创建我自己的。我能够让我的服务在使用 ASP.NET Developer Server 进行调试时运行良好。我打开端口 8081(在服务器、防火墙和路由器上),然后将其分配给 IIS 中的“注册”文件夹。在花了一天时间弄清楚我必须使用 MsDeployAgentService 之后,我将该服务发布到运行 IIS 6 的 Windows Server 2003 上。然后我使用 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 地址。

关注这篇文章是因为它看起来像 OP遵循相同的示例并且有类似的问题,但它没有帮助。

然后我查看了服务器上的事件查看器,当我包含在端口号中时发现了这一点:

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;
        }

    }
}
4

2 回答 2

0

您的 UriTemplate 设置是否正确?404 错误表明它根本没有与服务/方法联系。

REST 服务是简单的 web api,而不是像普通 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();
于 2012-10-15T22:53:26.320 回答
0

将 IIS 6.0 用于服务时,我不需要在 URL 中包含文件夹名称。所以简单地改变一行代码就解决了我的问题。

byte[] data = client.DownloadData("http://ServerIP:8081/Service1.svc/CheckSoftware/1/Customer1/1_0/abc123/1/1");
于 2012-10-17T13:13:30.477 回答