Skip to content

Commit

Permalink
Update WCF sample Instancing/Lifetime to conditionally support .NET f…
Browse files Browse the repository at this point in the history
…ramework / Core
  • Loading branch information
afifi-ins committed Oct 31, 2023
1 parent 3499221 commit 7c51535
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 302 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
</bindings>

</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
</configuration>
32 changes: 17 additions & 15 deletions framework/wcf/Extensibility/Instancing/Lifetime/CS/client/Client.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved.

using System;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Microsoft.ServiceModel.Samples
{
[ServiceContract(SessionMode=SessionMode.Required)]
[ServiceContract(SessionMode = SessionMode.Required)]
interface IEchoService
{
[OperationContract]
Expand All @@ -15,30 +15,32 @@ interface IEchoService

public static class CustomHeader
{
public static readonly String HeaderName = "InstanceId";
public static readonly String HeaderNamespace = "http://Microsoft.ServiceModel.Samples/Lifetime";
public static readonly string HeaderName = "InstanceId";
public static readonly string HeaderNamespace = "http://Microsoft.ServiceModel.Samples/Lifetime";
}

class Program
{
static void Main(string[] args)
{
Console.WriteLine(
"Press <enter> to open a channel and send a request.");
Console.WriteLine("Press <enter> to open a channel and send a request.");

Console.ReadLine();

MessageHeader shareableInstanceContextHeader = MessageHeader.CreateHeader(
CustomHeader.HeaderName,
CustomHeader.HeaderNamespace,
Guid.NewGuid().ToString());

ChannelFactory<IEchoService> channelFactory =
new ChannelFactory<IEchoService>("echoservice");

#if NET6_0_OR_GREATER
CustomBinding binding = new CustomBinding(new TextMessageEncodingBindingElement(), new TcpTransportBindingElement());
EndpointAddress endpointAddress = new EndpointAddress(new Uri("net.tcp://localhost:9000/echoservice"));
ChannelFactory<IEchoService> channelFactory = new ChannelFactory<IEchoService>(binding, endpointAddress);
#else
ChannelFactory<IEchoService> channelFactory = new ChannelFactory<IEchoService>("echoservice");
#endif
IEchoService proxy = channelFactory.CreateChannel();


using (new OperationContextScope((IClientChannel)proxy))
{
OperationContext.Current.OutgoingMessageHeaders.Add(shareableInstanceContextHeader);
Expand All @@ -52,8 +54,8 @@ static void Main(string[] args)
Console.WriteLine("Channel No 1 closed.");

Console.WriteLine(
"Press <ENTER> to send another request from a different channel " +
"to the same instance. ");
"Press <ENTER> to send another request from a different channel " +
"to the same instance. ");

Console.ReadLine();

Expand All @@ -66,11 +68,11 @@ static void Main(string[] args)
Console.WriteLine("Service returned: " + proxy.Echo("Apple"));
Console.ForegroundColor = ConsoleColor.Gray;
}

((IChannel)proxy).Close();

Console.WriteLine("Channel No 2 closed.");

Console.WriteLine("Press <ENTER> to complete test.");
Console.ReadLine();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,100 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{141A9C5A-0610-4F1B-BDD5-1EAF8416850E}</ProjectGuid>
<TargetFrameworks>net462;net6.0</TargetFrameworks>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Microsoft.ServiceModel.Samples</RootNamespace>
<AssemblyName>Client</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>2.0</OldToolsVersion>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile />
<Title>Client</Title>
<Description>Lifetime sample</Description>
<Company>Microsoft Corporation</Company>
<Product>Windows Communication Foundation and Windows Workflow Foundation SDK</Product>
<Copyright>Copyright (c) Microsoft Corporation</Copyright>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Client.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<None Remove="App.config" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />

<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<Reference Include="System.ServiceModel" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.VisualBasic.PowerPacks.10.0">
<Visible>False</Visible>
<ProductName>Microsoft Visual Basic PowerPacks 10.0</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
<Visible>False</Visible>
<ProductName>Windows Installer 3.1</ProductName>
<Install>true</Install>
</BootstrapperPackage>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<PackageReference Include="System.ServiceModel.Primitives" Version="6.*" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="6.*" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\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>
</Project>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@
#region using

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;
using System.Timers;
using Timer = System.Timers.Timer;
using System.ServiceModel.Dispatcher;

#endregion

namespace Microsoft.ServiceModel.Samples
{
interface ICustomLease
{
bool IsIdle { get; }
bool IsIdle { get; }
InstanceContextIdleCallback Callback { get; set; }
}

Expand All @@ -32,15 +29,15 @@ class CustomLeaseExtension : IExtension<InstanceContext>, ICustomLease
// Reference to the InstanceContext instance owns this
// extension instance.
InstanceContext owner;

bool isIdle;
object thisLock;
Timer idleTimer;
double idleTimeout;
InstanceContextIdleCallback callback;
string instanceId;

public String InstanceId
public string InstanceId
{
get
{
Expand All @@ -52,7 +49,7 @@ public String InstanceId

#region Constructor

public CustomLeaseExtension(double idleTimeout, String instanceId)
public CustomLeaseExtension(double idleTimeout, string instanceId)
{
owner = null;
isIdle = false;
Expand All @@ -76,11 +73,11 @@ public CustomLeaseExtension(double idleTimeout, String instanceId)
/// </remarks>
public void Attach(InstanceContext owner)
{
this.owner = owner;
this.owner = owner;
}

public void Detach(InstanceContext owner)
{
{
}

#endregion
Expand Down Expand Up @@ -109,7 +106,7 @@ public bool IsIdle
}
}
}

/// <summary>
/// Gets or sets the InstanceContextIdleCallback.
/// </summary>
Expand Down Expand Up @@ -183,9 +180,9 @@ void idleTimer_Elapsed(object sender, ElapsedEventArgs args)
ResourceHelper.GetString("MsgLeaseExpired"));
callback(owner);
}
}
}

#endregion

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved.

using System;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace Microsoft.ServiceModel.Samples
{
Expand Down Expand Up @@ -54,7 +54,7 @@ public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBas
foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
{
ChannelDispatcher cd = cdb as ChannelDispatcher;

if (cd != null)
{
foreach (EndpointDispatcher ed in cd.Endpoints)
Expand Down
Loading

0 comments on commit 7c51535

Please sign in to comment.