Skip to content

Commit

Permalink
增加新的Demo并且学习WCF基础功能
Browse files Browse the repository at this point in the history
  • Loading branch information
LU Yulong authored and LU Yulong committed Jul 12, 2017
1 parent 6f87846 commit cd46a3c
Show file tree
Hide file tree
Showing 26 changed files with 1,104 additions and 6 deletions.
11 changes: 11 additions & 0 deletions AspectDemo/AspectDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -51,6 +52,16 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\InterfaceLib\InterfaceLib.csproj">
<Project>{10835cba-a1cb-436e-82af-333d2d659c9f}</Project>
<Name>InterfaceLib</Name>
</ProjectReference>
<ProjectReference Include="..\WcfServiceDemo\WcfServiceDemo.csproj">
<Project>{bef64c1b-f4e9-451d-a6d7-3a81ac24a951}</Project>
<Name>WcfServiceDemo</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
80 changes: 74 additions & 6 deletions AspectDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,87 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using WcfServiceDemo;
using InterfaceLib;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;

namespace AspectDemo
{
class Program
{
static void Main(string[] args)
{
TestObject obj = new TestObject("Objetc1");
obj.PrintInfo();
obj.GetName();
obj.PrintInfo();
obj.SetName();
Console.ReadLine();
using (ServiceHost host = new ServiceHost(typeof(CWcfService)))
{
var header=AddressHeader.CreateAddressHeader("Test","http://fitsco.com.cn","Luyulong");
EndpointAddress address = new EndpointAddress(new Uri("net.pipe://localhost/Services/WcfService"), header);
Binding bind = new NetNamedPipeBinding();
ContractDescription contract=ContractDescription.GetContract(typeof(IWcfService));
ServiceEndpoint endpoint=new ServiceEndpoint(contract,bind,address);
endpoint.ListenUri = new Uri("net.pipe://localhost/Services/WcfService1");
host.AddServiceEndpoint(endpoint);

if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
{
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.HttpGetUrl = new Uri("http://localhost/Services/WcfService/Metadata");
host.Description.Behaviors.Add(behavior);
}
host.Opened += host_Opened;
host.Opening += host_Opening;
host.Faulted += host_Faulted;
host.UnknownMessageReceived += host_UnknownMessageReceived;
host.Closing += host_Closing;
host.Closed += host_Closed;
host.Open();
Console.ReadLine();
}
}

static void host_Closed(object sender, EventArgs e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Service is closed......");
Console.ResetColor();
}

static void host_Closing(object sender, EventArgs e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Service is closing......");
Console.ResetColor();
}

static void host_UnknownMessageReceived(object sender, UnknownMessageReceivedEventArgs e)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Service receive unknow data......");
Console.ResetColor();
}

static void host_Faulted(object sender, EventArgs e)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Comunication object convert error......");
Console.ResetColor();
}

static void host_Opening(object sender, EventArgs e)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Service is opening......");
Console.ResetColor();
}

static void host_Opened(object sender, EventArgs e)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Service is opened......");
Console.ResetColor();
}
}
}
18 changes: 18 additions & 0 deletions ContextDemo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProxyDemo", "ProxyDemo\Prox
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspectDemo", "AspectDemo\AspectDemo.csproj", "{5AA99E68-E425-4551-BC03-82F30D3825B2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WcfServiceDemo", "WcfServiceDemo\WcfServiceDemo.csproj", "{BEF64C1B-F4E9-451D-A6D7-3A81AC24A951}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WcfCallDemo", "WcfCallDemo\WcfCallDemo.csproj", "{9764C28D-F2B2-4AF8-B02E-FA2D825B3EE5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WcfHttpRefCall", "WcfHttpRefCall\WcfHttpRefCall.csproj", "{49D8A483-2259-420B-8781-57F12C0CA102}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -51,6 +57,18 @@ Global
{5AA99E68-E425-4551-BC03-82F30D3825B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5AA99E68-E425-4551-BC03-82F30D3825B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5AA99E68-E425-4551-BC03-82F30D3825B2}.Release|Any CPU.Build.0 = Release|Any CPU
{BEF64C1B-F4E9-451D-A6D7-3A81AC24A951}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BEF64C1B-F4E9-451D-A6D7-3A81AC24A951}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BEF64C1B-F4E9-451D-A6D7-3A81AC24A951}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BEF64C1B-F4E9-451D-A6D7-3A81AC24A951}.Release|Any CPU.Build.0 = Release|Any CPU
{9764C28D-F2B2-4AF8-B02E-FA2D825B3EE5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9764C28D-F2B2-4AF8-B02E-FA2D825B3EE5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9764C28D-F2B2-4AF8-B02E-FA2D825B3EE5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9764C28D-F2B2-4AF8-B02E-FA2D825B3EE5}.Release|Any CPU.Build.0 = Release|Any CPU
{49D8A483-2259-420B-8781-57F12C0CA102}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{49D8A483-2259-420B-8781-57F12C0CA102}.Debug|Any CPU.Build.0 = Debug|Any CPU
{49D8A483-2259-420B-8781-57F12C0CA102}.Release|Any CPU.ActiveCfg = Release|Any CPU
{49D8A483-2259-420B-8781-57F12C0CA102}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
19 changes: 19 additions & 0 deletions InterfaceLib/IWcfService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace InterfaceLib
{
[ServiceContract]
public interface IWcfService
{
[OperationContract]
int GetData();

[OperationContract]
void SetData(int p1, int p2);
}
}
2 changes: 2 additions & 0 deletions InterfaceLib/InterfaceLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -40,6 +41,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="IMatch.cs" />
<Compile Include="IWcfService.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
6 changes: 6 additions & 0 deletions WcfCallDemo/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
35 changes: 35 additions & 0 deletions WcfCallDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using InterfaceLib;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;

namespace WcfCallDemo
{
class Program
{
static void Main(string[] args)
{
AddressHeader header = AddressHeader.CreateAddressHeader("Test", "http://fitsco.com.cn", "Luyulong");
EndpointAddress address = new EndpointAddress(new Uri("net.pipe://localhost/Services/WcfService"), header);
Binding bind = new NetNamedPipeBinding();
ContractDescription contract = ContractDescription.GetContract(typeof(IWcfService));
ServiceEndpoint endpoint = new ServiceEndpoint(contract, bind, address);
ClientViaBehavior clientBehavior = new ClientViaBehavior(new Uri("net.pipe://localhost/Services/WcfService1"));
endpoint.EndpointBehaviors.Add(clientBehavior);
ChannelFactory<IWcfService> factory = new ChannelFactory<IWcfService>(endpoint);

var obj = factory.CreateChannel();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(obj.GetData());
obj.SetData(12, 18);
Console.WriteLine(obj.GetData());
Console.ResetColor();
Console.Read();
}
}
}
36 changes: 36 additions & 0 deletions WcfCallDemo/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// 有关程序集的常规信息通过以下
// 特性集控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("WcfCallDemo")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("WcfCallDemo")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// 将 ComVisible 设置为 false 使此程序集中的类型
// 对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
// 则将该类型上的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]

// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("d85c2d67-b15c-4bb8-a22c-ebd00e75cd3d")]

// 程序集的版本信息由下面四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
65 changes: 65 additions & 0 deletions WcfCallDemo/WcfCallDemo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{9764C28D-F2B2-4AF8-B02E-FA2D825B3EE5}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>WcfCallDemo</RootNamespace>
<AssemblyName>WcfCallDemo</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\InterfaceLib\InterfaceLib.csproj">
<Project>{10835cba-a1cb-436e-82af-333d2d659c9f}</Project>
<Name>InterfaceLib</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
25 changes: 25 additions & 0 deletions WcfHttpRefCall/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<netNamedPipeBinding>
<binding name="NetNamedPipeBinding_IWcfService" />
</netNamedPipeBinding>
</bindings>
<client>
<endpoint address="net.pipe://localhost/Services/WcfService"
binding="netNamedPipeBinding" bindingConfiguration="NetNamedPipeBinding_IWcfService"
contract="ServiceReference.IWcfService" name="NetNamedPipeBinding_IWcfService">
<headers>
<Test xmlns="http://fitsco.com.cn">Luyulong</Test>
</headers>
<identity>
<userPrincipalName value="[email protected]" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
23 changes: 23 additions & 0 deletions WcfHttpRefCall/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WcfHttpRefCall
{
class Program
{
static void Main(string[] args)
{
ServiceReference.WcfServiceClient obj = new ServiceReference.WcfServiceClient();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Blue;
Console.WriteLine(obj.GetData());
obj.SetData(12, 18);
Console.WriteLine(obj.GetData());
Console.ResetColor();
Console.Read();
}
}
}
Loading

0 comments on commit cd46a3c

Please sign in to comment.