Today I found how to do settings for impersonation and configuration settings for impersonation.
This will be usuaful for the developers who are working on WCF.
Here I will expalin with the code
Server Code
In this I have two methods
One to create a text file, at the same time it will add some message ti tat text file
Other method is to read string from that text file.
In client Application I am calling these two methods.
[OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
public void WriteToFile(string sText) This will create a text file and add sText data to that file.
{
FileInfo temp = new FileInfo("Hello.txt");
StreamWriter text = temp.CreateText();
text.WriteLine(sText);
text.Close();
Console.WriteLine(sText);
}
If you don't use ImpersonationOption.Allowed, It will give impersonation exception.
If you are not dealing with the files you don't need to add impersonation property.
[OperationBehavior(Impersonation = ImpersonationOption.Allowed)]
public string ReadFromFile() This method will read data from Hello.Txt file and return that string
{
Console.WriteLine("End of Writing");
StreamReader read = File.OpenText("Hello.txt");
string sRead = null;
sRead = read.ReadLine();
if (sRead == null)
return "Battula";
else
return sRead + " Battula";
}
configuration files contains normal client server configuration.
I used netTcpBinding binding.
server configuration file
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MetadataBehavior">
<serviceThrottling maxConcurrentCalls="1" maxConcurrentSessions="1" maxConcurrentInstances="1"/>
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:6010/clsFiles1/" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MetadataBehavior" name="Files1.clsFiles1">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
name="battula" contract="Files1.IFiles1" />
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
name="ep2" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:6010/clsFiles1/" />
<add baseAddress="net.tcp://localhost:6011/clsFiles1/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
The Client Configuration file I used
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="battula" closeTimeout="00:01:00" openTimeout="00:01:00"
receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false"
transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10"
maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:6011/clsFiles1/"
binding="netTcpBinding" bindingConfiguration="battula" contract="IFiles1"
name="battula">
<identity>
<userPrincipalName value="BBO-XP\bbo" />
</identity>
</endpoint>
</client>
</system.serviceModel>
The client apllication what I tested is very simple and basic one. I have two command buttions and two text boxes. One command buttion is create a text file and it can write some content into that text file, that content we have to write in text box.
The other command button read data from that text file an write that content into the other text box.
Code
namespace FilesClient
{
public partial class Form1 : Form
{
Files1Client m_client = new Files1Client();
public Form1()
{
InitializeComponent();
}
private void cmdWrite_Click(object sender, EventArgs e)
{
string str = txtWrite.Text;
m_client.WriteToFile(str);
}
private void cmdRead_Click(object sender, EventArgs e)
{
string str =m_client.ReadFromFile();
txtRead.Text = str;
}
}
}
Regards
Ravi.Battula