【问题标题】:Not Getting the Data in URL by Web Service没有通过 Web 服务获取 URL 中的数据
【发布时间】:2015-01-14 11:38:14
【问题描述】:

我正在制作网络服务,但没有通过 URL 获取解析数据 WEB SERVICE的代码是这个。我的 IService 类

  namespace DataService
 {

   [ServiceContract]
   public interface IService1
  {

     [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedResponse)]
    List<RequestData> GetUser(RequestData data);

    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "UsersList/{id}", RequestFormat = WebMessageFormat.Json)]
    RequestData UsersList(string id);

     }


  [DataContract]
   public class RequestData 
   {
    [DataMember] 
    public string Name { get; set; } 
    [DataMember] 
    public int Age { get; set; } 
    [DataMember] 
    public string Address { get; set; }
}

}

这是我的 service1 类由 Iservice1 类继承

namespace DataService
{

    public class Service1 : IService1
   {

      public List<RequestData> GetUser(RequestData data)
      {
        List<RequestData> list = new List<RequestData>();
        if (data.Name.ToUpper() == "MAIRAJ")
        {
            list.Add(new RequestData
            {
                Name = "Mairaj",
                Age = 25,
                Address = "Test Address"
            });
            list.Add(new RequestData
            {
                Name = "Ahmad",
                Age = 25,
                Address = "Test Address"
            });
            list.Add(new RequestData
            {
                Name = "Minhas",
                Age = 25,
                Address = "Test Address"
            });
        }
        return list;
    }
    public RequestData UsersList(string userId)
    {
        if (userId == "1")
        {
            return new RequestData
            {
                Name = "Mairaj",
                Age = 25,
                Address = "Test Address"
            };
        }
        else
        {
            return new RequestData
            {
                Name = "Amir",
                Age = 25,
                Address = "Test Address"
            };
        }
        }
       }
    }

我在部署 Web 服务 http://116.58.61.180/ADG/Service1.svc 后提供此 URL 应该解析什么确切的 url 来获取数据

这是我的 web.config

   <?xml version="1.0"?>
  <configuration>

   <system.web>
  <compilation debug="true" targetFramework="4.0" />
   </system.web>
  <system.serviceModel>
   <behaviors>
   <serviceBehaviors>
      <behavior>

      <serviceMetadata httpGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
    </serviceBehaviors>
  </behaviors>
  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
   </system.serviceModel>

  <directoryBrowse enabled="true"/>
  </system.webServer>

  </configuration>

【问题讨论】:

  • 你能发布你的 web.config 吗?
  • @Pankaj 是的,我正在发布这个
  • @Pankaj 仍然没有得到任何东西
  • 我已经更新了答案集中的说明​​。

标签: c# web-services url


【解决方案1】:

我认为您只是忘记了 web.config 中的一些内容:

<endpointBehaviors>
    <behavior>
        <webHttp helpEnabled="true"/>
    </behavior>
</endpointBehaviors>

<protocolMapping>
   <add binding="webHttpBinding" scheme="http" />
</protocolMapping>

如果你不把所有这些东西都放在你的 web.config 中,你将无法让你的服务正常工作。

完整的 Web.config 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
        <endpointBehaviors>
            <behavior>
                <webHttp helpEnabled="true"/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="webHttpBinding" scheme="http" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

有关更多详细信息,我几个月前在我的博客上写过一篇关于 WCF 和 REST 的文章:

Simple WCF and REST service

WCF and POST method

【讨论】:

【解决方案2】:

按照以下步骤操作:

  1. 使用以下属性装饰您的服务类

    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1
    
  2. Web.config 应该如下所示

      <?xml version="1.0"?>
        <configuration>
          <system.web>
             <compilation debug="true" targetFramework="4.0" />
          </system.web>
          <system.webServer>
             <modules runAllManagedModulesForAllRequests="true">
                <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
             </modules>
          </system.webServer>
          <system.serviceModel>
            <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
              <standardEndpoints>
                <webHttpEndpoint>
                  <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
                </webHttpEndpoint>
              </standardEndpoints>
          </system.serviceModel>
        </configuration>
    
  3. 您的 Global.asax 应该如下所示。

    public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
          RegisterRoutes();
        }
    
        private void RegisterRoutes()
        {
        // Edit the base address of Service1 by replacing the "Service1" string below
            RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
        }
    }
    

【讨论】:

  • 请告诉我应该在服务类中添加哪个程序集。因为我在这一行的 AspNetCompatibilityRequirements 中遇到错误
  • Global.asax 1.创建一个空的新项目。 2.转到该文件夹​​并复制 Global.asax 文件。 3.转到您的项目并在解决方案资源管理器中将其粘贴到根目录。 4.打开 Global.asax 文件并将命名空间匹配更改为您的命名空间。 5.在解决方案资源管理器中右键单击 Global.asax 并选择查看标记并更改继承匹配到您的命名空间。 AspNetCompatibilityRequirements : 1.Assembly:System.ServiceModel 2.命名空间:System.ServiceModel.Activation
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-04
  • 2013-08-24
  • 2014-07-08
  • 2014-02-05
相关资源
最近更新 更多