Skip to content

Commit

Permalink
MVC app calls WCF. WCF illustrates standard WCF and WCF-REST.
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Illig committed Oct 5, 2016
1 parent ea2a1d3 commit 4116679
Show file tree
Hide file tree
Showing 35 changed files with 825 additions and 179 deletions.
3 changes: 3 additions & 0 deletions Examples.sln
Expand Up @@ -16,6 +16,9 @@ EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AttributeMetadataExample", "src\AttributeMetadataExample\AttributeMetadataExample.csproj", "{38DDAE6D-0D1D-40D6-BF57-CAFED0322222}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MvcExample", "src\MvcExample\MvcExample.csproj", "{49C746F1-53F4-433C-ABE0-1938D8B508B2}"
ProjectSection(ProjectDependencies) = postProject
{91212914-7EAF-4FC7-88E8-5C8ED9A9DB4A} = {91212914-7EAF-4FC7-88E8-5C8ED9A9DB4A}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WcfExample", "src\WcfExample\WcfExample.csproj", "{91212914-7EAF-4FC7-88E8-5C8ED9A9DB4A}"
EndProject
Expand Down
20 changes: 17 additions & 3 deletions src/MvcExample/Controllers/HomeController.cs
@@ -1,24 +1,38 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using MvcExample.HostFactoryService;
using MvcExample.Models;

namespace MvcExample.Controllers
{
public class HomeController : Controller
{
public HomeController(IService serviceClient)
{
this.ServiceClient = serviceClient;
}

public IService ServiceClient { get; private set; }

public ActionResult About()
{
return View();
}

[CustomActionFilter]
public ActionResult Index()
public async Task<ActionResult> Index()
{
var serviceInfo = await this.ServiceClient.GetServiceInfoAsync(new GetServiceInfoRequest());

var model = new DependencyValueModel
{
// Injected by the custom action filter.
FilterValue = (long)this.HttpContext.Items["filterValue"]
// Comes from a dependency in the custom action filter.
FilterValue = (long)this.HttpContext.Items["filterValue"],

// Comes from a dependency injected into the WCF service.
WcfServiceDependencyId = serviceInfo.DependencyInstanceId
};

return View(model);
Expand Down
12 changes: 12 additions & 0 deletions src/MvcExample/Global.asax.cs
@@ -1,12 +1,15 @@
using System;
using System.Linq;
using System.Reflection;
using System.ServiceModel;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.Mvc;
using Autofac.Integration.Wcf;
using MvcExample.Dependencies;
using MvcExample.HostFactoryService;

namespace MvcExample
{
Expand Down Expand Up @@ -59,6 +62,15 @@ protected void Application_Start()
// MVC - OPTIONAL: Enable property injection into action filters.
builder.RegisterFilterProvider();

// WCF - Register channel factory and channel for service clients.
builder
.Register(c => new ChannelFactory<IService>("BasicHttpBinding_IService"))
.SingleInstance();
builder
.Register(c => c.Resolve<ChannelFactory<IService>>().CreateChannel())
.As<IService>()
.UseWcfSafeRelease();

// Register application dependencies.
builder.RegisterType<ViewDependency>().As<IViewDependency>();
builder.RegisterType<FilterDependency>().As<IFilterDependency>();
Expand Down
8 changes: 8 additions & 0 deletions src/MvcExample/Models/DependencyValueModel.cs
Expand Up @@ -15,5 +15,13 @@ public class DependencyValueModel
/// An <see cref="System.Int64"/> with the current date and time as ticks.
/// </value>
public long FilterValue { get; set; }

/// <summary>
/// Gets or sets the ID of the WCF service dependency.
/// </summary>
/// <value>
/// The <see cref="Guid"/> that identifies the dependency injected into the WCF service.
/// </value>
public Guid WcfServiceDependencyId { get; set; }
}
}
35 changes: 34 additions & 1 deletion src/MvcExample/MvcExample.csproj
Expand Up @@ -56,6 +56,10 @@
<HintPath>..\..\packages\Autofac.Mvc5.4.0.0\lib\net451\Autofac.Integration.Mvc.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Autofac.Integration.Wcf, Version=4.0.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\..\packages\Autofac.Wcf.4.0.0\lib\net45\Autofac.Integration.Wcf.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.2\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
<Private>True</Private>
Expand All @@ -68,6 +72,8 @@
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" />
Expand Down Expand Up @@ -136,11 +142,26 @@
</Compile>
<Compile Include="Models\DependencyValueModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Service References\HostFactoryService\Reference.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Reference.svcmap</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<Content Include="favicon.ico" />
<Content Include="Global.asax" />
<Content Include="Content\Site.css" />
<None Include="Service References\HostFactoryService\HostFactoryService.disco" />
<None Include="Service References\HostFactoryService\configuration91.svcinfo" />
<None Include="Service References\HostFactoryService\configuration.svcinfo" />
<Content Include="Service References\HostFactoryService\MvcExample.HostFactoryService.GetServiceInfoResponse.datasource">
<DependentUpon>Reference.svcmap</DependentUpon>
</Content>
<None Include="Service References\HostFactoryService\Reference.svcmap">
<Generator>WCF Proxy Generator</Generator>
<LastGenOutput>Reference.cs</LastGenOutput>
</None>
<Content Include="Web.config" />
<Content Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
Expand All @@ -153,14 +174,26 @@
<Content Include="Views\Shared\Error.cshtml" />
<Content Include="Views\Shared\_Layout.cshtml" />
<Content Include="Views\Home\Index.cshtml" />
<None Include="Service References\HostFactoryService\HostFactoryService.wsdl" />
<None Include="Service References\HostFactoryService\HostFactoryService.xsd">
<SubType>Designer</SubType>
</None>
<None Include="Service References\HostFactoryService\HostFactoryService1.xsd">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Content Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Content Include="Views\Home\About.cshtml" />
</ItemGroup>
<ItemGroup>
<WCFMetadataStorage Include="Service References\HostFactoryService\" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
Expand Down
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<discovery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/disco/">
<contractRef ref="http://localhost:25665/HostFactoryService.svc?wsdl" docRef="http://localhost:25665/HostFactoryService.svc" xmlns="http://schemas.xmlsoap.org/disco/scl/" />
</discovery>
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:tns="http://tempuri.org/" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HostFactoryService" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="http://localhost:25665/HostFactoryService.svc?xsd=xsd0" namespace="http://tempuri.org/" />
<xsd:import schemaLocation="http://localhost:25665/HostFactoryService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
</xsd:schema>
</wsdl:types>
<wsdl:message name="IService_GetServiceInfo_InputMessage" />
<wsdl:message name="GetServiceInfoResponse">
<wsdl:part name="parameters" element="tns:GetServiceInfoResponse" />
</wsdl:message>
<wsdl:portType name="IService">
<wsdl:operation name="GetServiceInfo">
<wsdl:input wsaw:Action="http://tempuri.org/IService/GetServiceInfo" message="tns:IService_GetServiceInfo_InputMessage" />
<wsdl:output wsaw:Action="http://tempuri.org/IService/GetServiceInfoResponse" name="GetServiceInfoResponse" message="tns:GetServiceInfoResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpBinding_IService" type="tns:IService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="GetServiceInfo">
<soap:operation soapAction="http://tempuri.org/IService/GetServiceInfo" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="GetServiceInfoResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HostFactoryService">
<wsdl:port name="BasicHttpBinding_IService" binding="tns:BasicHttpBinding_IService">
<soap:address location="http://localhost:25665/HostFactoryService.svc" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://tempuri.org/" elementFormDefault="qualified" targetNamespace="http://tempuri.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:import schemaLocation="http://localhost:25665/HostFactoryService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
<xs:element name="GetServiceInfoResponse">
<xs:complexType>
<xs:sequence>
<xs:element xmlns:q1="http://schemas.microsoft.com/2003/10/Serialization/" minOccurs="0" name="DependencyInstanceId" type="q1:guid" />
<xs:element minOccurs="0" name="ServiceImplementationTypeName" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="anyType" nillable="true" type="xs:anyType" />
<xs:element name="anyURI" nillable="true" type="xs:anyURI" />
<xs:element name="base64Binary" nillable="true" type="xs:base64Binary" />
<xs:element name="boolean" nillable="true" type="xs:boolean" />
<xs:element name="byte" nillable="true" type="xs:byte" />
<xs:element name="dateTime" nillable="true" type="xs:dateTime" />
<xs:element name="decimal" nillable="true" type="xs:decimal" />
<xs:element name="double" nillable="true" type="xs:double" />
<xs:element name="float" nillable="true" type="xs:float" />
<xs:element name="int" nillable="true" type="xs:int" />
<xs:element name="long" nillable="true" type="xs:long" />
<xs:element name="QName" nillable="true" type="xs:QName" />
<xs:element name="short" nillable="true" type="xs:short" />
<xs:element name="string" nillable="true" type="xs:string" />
<xs:element name="unsignedByte" nillable="true" type="xs:unsignedByte" />
<xs:element name="unsignedInt" nillable="true" type="xs:unsignedInt" />
<xs:element name="unsignedLong" nillable="true" type="xs:unsignedLong" />
<xs:element name="unsignedShort" nillable="true" type="xs:unsignedShort" />
<xs:element name="char" nillable="true" type="tns:char" />
<xs:simpleType name="char">
<xs:restriction base="xs:int" />
</xs:simpleType>
<xs:element name="duration" nillable="true" type="tns:duration" />
<xs:simpleType name="duration">
<xs:restriction base="xs:duration">
<xs:pattern value="\-?P(\d*D)?(T(\d*H)?(\d*M)?(\d*(\.\d*)?S)?)?" />
<xs:minInclusive value="-P10675199DT2H48M5.4775808S" />
<xs:maxInclusive value="P10675199DT2H48M5.4775807S" />
</xs:restriction>
</xs:simpleType>
<xs:element name="guid" nillable="true" type="tns:guid" />
<xs:simpleType name="guid">
<xs:restriction base="xs:string">
<xs:pattern value="[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}" />
</xs:restriction>
</xs:simpleType>
<xs:attribute name="FactoryType" type="xs:QName" />
<xs:attribute name="Id" type="xs:ID" />
<xs:attribute name="Ref" type="xs:IDREF" />
</xs:schema>
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file is automatically generated by Visual Studio .Net. It is
used to store generic object data source configuration information.
Renaming the file extension or editing the content of this file may
cause the file to be unrecognizable by the program.
-->
<GenericObjectDataSource DisplayName="GetServiceInfoResponse" Version="1.0" xmlns="urn:schemas-microsoft-com:xml-msdatasource">
<TypeInfo>MvcExample.HostFactoryService.GetServiceInfoResponse, Service References.HostFactoryService.Reference.cs.dll, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null</TypeInfo>
</GenericObjectDataSource>
98 changes: 98 additions & 0 deletions src/MvcExample/Service References/HostFactoryService/Reference.cs
@@ -0,0 +1,98 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace MvcExample.HostFactoryService {


[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="HostFactoryService.IService")]
public interface IService {

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService/GetServiceInfo", ReplyAction="http://tempuri.org/IService/GetServiceInfoResponse")]
MvcExample.HostFactoryService.GetServiceInfoResponse GetServiceInfo(MvcExample.HostFactoryService.GetServiceInfoRequest request);

// CODEGEN: Generating message contract since the operation has multiple return values.
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService/GetServiceInfo", ReplyAction="http://tempuri.org/IService/GetServiceInfoResponse")]
System.Threading.Tasks.Task<MvcExample.HostFactoryService.GetServiceInfoResponse> GetServiceInfoAsync(MvcExample.HostFactoryService.GetServiceInfoRequest request);
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class GetServiceInfoRequest {

public GetServiceInfoRequest() {
}
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.MessageContractAttribute(WrapperName="GetServiceInfoResponse", WrapperNamespace="http://tempuri.org/", IsWrapped=true)]
public partial class GetServiceInfoResponse {

[System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tempuri.org/", Order=0)]
public System.Guid DependencyInstanceId;

[System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tempuri.org/", Order=1)]
public string ServiceImplementationTypeName;

public GetServiceInfoResponse() {
}

public GetServiceInfoResponse(System.Guid DependencyInstanceId, string ServiceImplementationTypeName) {
this.DependencyInstanceId = DependencyInstanceId;
this.ServiceImplementationTypeName = ServiceImplementationTypeName;
}
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public interface IServiceChannel : MvcExample.HostFactoryService.IService, System.ServiceModel.IClientChannel {
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class ServiceClient : System.ServiceModel.ClientBase<MvcExample.HostFactoryService.IService>, MvcExample.HostFactoryService.IService {

public ServiceClient() {
}

public ServiceClient(string endpointConfigurationName) :
base(endpointConfigurationName) {
}

public ServiceClient(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress) {
}

public ServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress) {
}

public ServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress) {
}

[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
MvcExample.HostFactoryService.GetServiceInfoResponse MvcExample.HostFactoryService.IService.GetServiceInfo(MvcExample.HostFactoryService.GetServiceInfoRequest request) {
return base.Channel.GetServiceInfo(request);
}

public System.Guid GetServiceInfo(out string ServiceImplementationTypeName) {
MvcExample.HostFactoryService.GetServiceInfoRequest inValue = new MvcExample.HostFactoryService.GetServiceInfoRequest();
MvcExample.HostFactoryService.GetServiceInfoResponse retVal = ((MvcExample.HostFactoryService.IService)(this)).GetServiceInfo(inValue);
ServiceImplementationTypeName = retVal.ServiceImplementationTypeName;
return retVal.DependencyInstanceId;
}

public System.Threading.Tasks.Task<MvcExample.HostFactoryService.GetServiceInfoResponse> GetServiceInfoAsync(MvcExample.HostFactoryService.GetServiceInfoRequest request) {
return base.Channel.GetServiceInfoAsync(request);
}
}
}

0 comments on commit 4116679

Please sign in to comment.