当svc和cs文件保存在用户创建的文件夹下时,Restful WCF无法正常工作

时间:2017-01-23 05:36:11

标签: c# asp.net .net wcf wcf-rest

我使用Restful WCF called MyRESTService创建了restfulWCF服务 如果我不把我的RESTfulWCF放在任何文件夹下并且不使用任何命名空间它可以工作,但是如果我使用MyRESTServiceTest.services.RESTfulWCF.ProductRESTService这样的命名空间并将我的WCF文件放在两个文件夹下,它就不起作用并且给我一些错误

“MyRESTService.services.RESTfulWCF.ProductRESTService”类型,作为ServiceHost指令中的Service属性值提供,或者在配置元素system.serviceModel / serviceHostingEnvironment / serviceActivations中提供。

我在项目中创建了2个文件夹(MyRESTService) - > RESTfulWCF 并将我的所有WCF文件放在我的项目中的RESTfulWCF文件夹下。然后我的WCF停止在我的本地机器上工作。我的WCF名称是ProductRESTService 使用接口IProductRESTService,ProductRESTService.svc和ProductRESTService.svc.cs和Products.cs文件 enter image description here

ProductRESTService.svc

<%@ ServiceHost Language="C#" Debug="true" Service="MyRESTService.services.RESTfulWCF.ProductRESTService"   %>

ProductRESTService.svc.cs

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace MyRESTServiceTest.services.RESTfulWCF.ProductRESTService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ProductRESTService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select ProductRESTService.svc or ProductRESTService.svc.cs at the Solution Explorer and start debugging.
    public class ProductRESTService : IProductRESTService
    {
        public List<Product> GetProductList()
        {
            return Products.Instance.ProductList;
        }

    }
}

Products.cs

 using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    using System.ServiceModel;
    using System.Runtime.Serialization;


    namespace MyRESTServiceTest.services.RESTfulWCF.ProductRESTService
    {
        [DataContract]
        public class Product
        {
            [DataMember]
            public int ProductId { get; set; }
            [DataMember]
            public string Name { get; set; }
            [DataMember]
            public string CategoryName { get; set; }
            [DataMember]
            public int Price { get; set; }
        }

        public partial class Products
        {
           private static readonly Products _instance = new Products();

           private Products() { } 

           public static Products Instance 
           { 
                    get { return _instance; } 
           } 
            public List<Product> ProductList 
            { 
                   get { return products; } 
            } 
            private List<Product> products = new List<Product>() 
            { 
                    new Product() { ProductId = 1, Name = "Product 1", CategoryName = "Category 1", Price=10}, 
                    new Product() { ProductId = 1, Name = "Product 2", CategoryName = "Category 2", Price=5}, 
                    new Product() { ProductId = 1, Name = "Product 3", CategoryName = "Category 3", Price=15}, 
                    new Product() { ProductId = 1, Name = "Product 4", CategoryName = "Category 1", Price=9} 
            }; 
        }
    }

IProductService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Collections;
using System.Text;

namespace MyRESTServiceTest.services.RESTfulWCF.ProductRESTService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IProductRESTService" in both code and config file together.
    [ServiceContract]
    public interface IProductRESTService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml,
                                   BodyStyle = WebMessageBodyStyle.Bare,
                                   UriTemplate = "GetProductList/")]
        List<Product> GetProductList();
    }
}

的Web.config

    <?xml version="1.0"?>
<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>
    <services>
        <service name="MyRESTServiceTest.services.RESTfulWCF.ProductRESTService.ProductRESTService" behaviorConfiguration="serviceBehavior">
            <endpoint address=""
                      binding="webHttpBinding"
                      contract="MyRESTServiceTest.services.RESTfulWCF.ProductRESTService.IProductRESTService"
                      behaviorConfiguration="web"></endpoint>
             <host>
       <baseAddresses>
         <add baseAddress="http://localhost:55379/" />
       </baseAddresses>
     </host>

          </service>
   </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
                  <serviceMetadata httpGetEnabled="false"/>
                  <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>        
      </serviceBehaviors>
      <endpointBehaviors>
          <behavior name="web">
                <webHttp/>
            </behavior>
      </endpointBehaviors>
    </behaviors>
  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <security>
      <requestFiltering>
        <fileExtensions allowUnlisted="true">
          <remove fileExtension="." />
          <add fileExtension="." allowed="true" />
        </fileExtensions>
      </requestFiltering>
    </security>

    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

0 个答案:

没有答案