2013-03-01 15 views
9

Mam roztwór z 3 projektów:Hosting WCF Services w ASP.NET MVC projektu

  1. ConsoleClient (do testowania serwisu WCF)
  2. ServiceLibrary (dla WCF)
  3. Web (asp.net mvc projekt)

zrobiłem niektóre ustawienia w moim projekcie ServiceLibrary w 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> 

Kiedy uruchamiam ten projekt, wszystko wydaje się normalnie używać klienta testowego wcf.

Dodałem także WcfDataServiceTest.svc do mojego hosta Web project(mvc) do obsługi mojej usługi wcf.

Więc moje pytania to:

  1. jaka konfiguracja jest potrzebne dla mojego projektu WWW (web.config) faktycznie gospodarza usług wcf?
  2. A następnie chcę uruchomić aplikację konsolową, aby ją przetestować?

Uwaga: Ja testowałem moją usługę korzystając projekt konsoli ale to był już generację proxy z WCF klienta testowego.

Nawiasem mówiąc, plik wcfDataServiceTest.svc wygląda następująco:

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

Odpowiedz

14

jestem gospodarzem usługę WCF bezpośrednio w moim projekcie MVC. Poniżej jest ogólnym przykładem tego, jak to jest skonstruowane: konfiguracja usługi

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> 

oto klasy usługi:

[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(); 
    } 
} 

A tu, gdzie jestem zarejestrowanie go w moim MVC 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

nie mogę znaleźć ServiceRoute. Ktoś zna przestrzeń nazw? – Rap

+0

Rap, przestrzeń nazw (i dll) dla ServiceRoute to System.ServiceModel.Activation. Będziesz także potrzebować biblioteki System.ServiceModel.Web dla WebServiceHostFactory. –

1

Here jest blogu z opisem jak Usługa dd WCF w aplikacji ASP.NET MVC 4.

Należy dodać:

<endpointBehaviors> 
    <behavior name="restBehavior"> 
     <webHttp /> 
    </behavior> 
    </endpointBehaviors> 
Powiązane problemy