2013-03-01 28 views
9

Tôi có một giải pháp với 3 dự án:Hosting Dịch vụ WCF trong Asp.Net MVC Project

  1. ConsoleClient (để thử nghiệm dịch vụ WCF)
  2. ServiceLibrary (cho WCF)
  3. web (asp.net MVC dự án)

tôi đã làm một số cài đặt trong dự án ServiceLibrary tôi trong app.config

<system.serviceModel> 
    <services> 
     <service name="MrDAStoreJobs.ServiceLibrary.AdvertisementService"> 
     <clear /> 
     <endpoint address="http://localhost:8050/ServiceLibrary/basic" binding="basicHttpBinding" bindingConfiguration="" contract="MrDAStoreJobs.ServiceLibrary.Interface.IAdvertisementService" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8050/Design_Time_Addresses/MrDAStoreJobs/ServiceLibrary/AdvertisementService/" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, 
      set the value below to false before deployment --> 
      <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> 
    </system.serviceModel> 

Khi tôi chạy dự án này, mọi thứ có vẻ bình thường khi sử dụng trình kiểm tra khách hàng wcf.

Bây giờ, tôi cũng đã thêm WcfDataServiceTest.svc vào số Web project(mvc) để lưu trữ dịch vụ wcf của mình.

Vì vậy, câu hỏi của tôi là:

  1. những gì cấu hình sao tôi cần cho dự án web của tôi (web.config) để thực sự tổ chức dịch vụ WCF này?
  2. Và sau đó tôi muốn chạy ứng dụng bảng điều khiển để kiểm tra nó?

Lưu ý: Tôi đã thử nghiệm dịch vụ của mình bằng cách sử dụng dự án bảng điều khiển nhưng đã nhận được proxy từ khách hàng thử nghiệm WCF.

Bằng cách này, các tập tin wcfDataServiceTest.svc trông như thế này:

public class WcfDataServiceTest : DataService<AdvertisementService> 
{ 
    // This method is called only once to initialize service-wide policies. 
    public static void InitializeService(DataServiceConfiguration config) 
    { 
     // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc. 
     // Examples: 
     config.SetEntitySetAccessRule("Advertisements", EntitySetRights.AllRead); 
     // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All); 
     config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3; 
    } 
} 

Trả lời

14

Tôi đang lưu trữ một dịch vụ WCF trực tiếp trong dự án MVC của tôi. Dưới đây là một ví dụ chung chung như thế nào nó có cấu trúc: cấu hình dịch vụ

Web.config:

<system.serviceModel> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name=""> 
     <serviceMetadata httpGetEnabled="true" /> 
     <serviceDebug includeExceptionDetailInFaults="false" /> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<bindings> 
    <customBinding> 
    <binding name="customBinding0"> 
     <binaryMessageEncoding /> 
     <httpTransport /> 
    </binding> 
    </customBinding> 
</bindings> 
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
<standardEndpoints> 
    <webHttpEndpoint> 
    <standardEndpoint name="" helpEnabled="true" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true" maxReceivedMessageSize="1048576"> 
     <readerQuotas maxStringContentLength="1048576" /> 
    </standardEndpoint> 
    </webHttpEndpoint> 
</standardEndpoints> 
</system.serviceModel> 

Dưới đây là các lớp dịch vụ:

[ServiceContract] 
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class MyService 
{ 
    [OperationContract] 
    [WebInvoke(UriTemplate = "{personId}", Method = "GET")] 
    public Person Get(string personId) 
    { 
     return new Person(); 
    } 
} 

Và đây là nơi tôi đăng ký nó trong MVC của tôi Global.asax

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    RouteTable.Routes.Add(new ServiceRoute("SVC/My", new WebServiceHostFactory(), typeof(MyService))); 
} 
+0

tôi không thể tìm ServiceRoute. Bất cứ ai biết không gian tên? – Rap

+0

Rap, không gian tên (và dll) cho ServiceRoute là System.ServiceModel.Activation. Bạn cũng sẽ cần tệp System.ServiceModel.Web cho WebServiceHostFactory. –

1

Here là bài đăng trên blog có mô tả về cách dd Dịch vụ WCF trong ứng dụng ASP.NET MVC 4.

Bạn nên thêm:

<endpointBehaviors> 
    <behavior name="restBehavior"> 
     <webHttp /> 
    </behavior> 
    </endpointBehaviors> 
Các vấn đề liên quan