Hi @la07k-4914
First of all, I will give you the code:
Server port service class:
namespace Test
{
[ServiceContract(CallbackContract=typeof(ICallback))]
public interface ICalculator
{
[OperationContract]
void ReceiveStream(Stream a);
}
public interface ICallback
{
[OperationContract(IsOneWay = true)]
void Display(string Str);
}
public class CalculatorService : ICalculator
{
public void ReceiveStream(Stream a)
{
StreamReader s = new StreamReader(a);
string str = s.ReadLine();
s.Close();
ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
callback.Display(str);
}
}
}
Since my client doesn't need to get anything from the server, I set "Isoneway=true".
This is the callback method
ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>()
callback.Display(str);
This is my server startup class "Nettcpbinding":
class main
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
{
TcpTransportBindingElement transport = new TcpTransportBindingElement();
host.Open();
Console.WriteLine("work");
Console.ReadKey();
}
}
}
This is my app.config(Basic ABC protocol):
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8080/Metadata"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Test.CalculatorService">
<endpoint address="net.tcp://localhost:9003/CalculatorService" binding="netTcpBinding" contract="Test.ICalculator"/>
</service>
</services>
</system.serviceModel>
</configuration>
This is my client callback class:
[ServiceContract( CallbackContract=typeof(ICallback))]
public interface ICalculator
{
[OperationContract]
void ReceiveStream(Stream a);
}
public interface ICallback
{
[OperationContract(IsOneWay = true)]
void Display(string Str);
}
public class CalculatorService : ICalculator
{
public void ReceiveStream(Stream a)
{
StreamReader s = new StreamReader(a);
string str = s.ReadLine();
s.Close();
ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>();
callback.Display(str);
}
}
This is my client startup class
class Program
{
static void Main(string[] args)
{
InstanceContext instanceContex = new InstanceContext(new CallbackWCFService());
CalculatorClient proxy = new CalculatorClient(instanceContex);
FileStream f = new FileStream("C:\\Users\\Administrator\\Desktop\\a.txt", FileMode.OpenOrCreate);
proxy.ReceiveStream(f);
f.Close();
Console.Read();
}
}
These are the basic WCF client invocation patterns above:
Here are my test results:
I've implemented callback streaming for both the server and the client. Since you didn't give me your method, I wrote the method myself.If you have any questions, you can leave a message below.
Best Regards
Qi You
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.