Named pipe wrapper, usage over network - c#

I have an working implementation of how two form applications and use named-pipe-wrapper v1.5.
Connection like this on server:
server = new NamedPipeServer<string>("named_pipe_test_server");
Connection like this on client:
client = new NamedPipeClient<string>("named_pipe_test_server");
now i have my server on another computer, same network.
how do i adress this.
I want to type like,
client = new NamedPipeClient<string>("//192.168.100.2//named_pipe_test_server");
is this possible ? in this case, what is the syntax ?

You can use the NamedPipeClientStream(String, String) overload to specify the name of the remote computer.
client = new NamedPipeClientStream("192.168.100.2", "named_pipe_test_server");

In named-pipe-server you can specify the server name as the second parameter to NamedPipeClient.
private readonly NamedPipeClient<string> _client
= new NamedPipeClient<string>(Constants.PIPE_NAME,"SPAN24");
You must use the Computer name not it's IP address.
You need to get the source from Github.
https://github.com/acdvorak/named-pipe-wrapper
The second parameter is not available in the Nuget package.

Related

Cannot connect to Azure ServiceBus with Microsoft.Azure.ServiceBus

I have created a very simple console application that connects to Azure ServiceBus and sends one message. I tried the latest library from Microsoft (Microsoft.Azure.ServiceBus) but no matter what I do I just get this error:
No connection could be made because the target machine actively
refused it ErrorCode: ConnectionRefused
I have tried exactly the same connection string in Service Bus Explorer and it does work just fine. Moreover I connected without problems using the older library from Microsoft (WindowsAzure.ServiceBus).
var sender = new MessageSender("endpoint", "topicName");
sender.SendAsync(new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject("test"))));
I tried with .NET Framework 4.6.2 and Core, same exception. I suspect there may be some differences in the default protocol that these libraries use, but I could not figure out that for sure.
P.S. Have tried the example from Microsoft docs but result is still the same exception
The old client supported ConnectivityMode using TCP, HTTP, HTTPS, and AutoDetect. ServiceBus Explorer is using AutoDetect, trying TCP first and then failing over to HTTPS, regardless of the TransportMode you were using (SBMP or AMQP).
With the new client this has changed. TransportMode now combines both options and offers Amqp (AMQP over TCP) or AmqpWebSockets (AMQP over WebSockets). There's no AutoDetect mode. You will have to create your clients and specify TransportType as AmqpWebSockets to bypass blocked TCP port 5671 and instead use port 443.
It seems that the documentation is lacking a lot on how to connect using HTTPS (Amqp over WebSockets) but after some help from Sean Feldman in the accepted answer I managed to connect. Here is the code that I used if someone is interested:
var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(
"RootManageSharedAccessKey", // SharedAccessKeyName
"SomeToken");
var sender = new MessageSender(
"sb://mydomain.servicebus.windows.net/",
"topicName",
tokenProvider,
TransportType.AmqpWebSockets);
Or a variant that let's you have the whole connection string in one piece
var builder = new ServiceBusConnectionStringBuilder("YouConnectionString");
var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(
builder.SasKeyName,
builder.SasKey);
var sender = new MessageSender(
builder.Endpoint,
"TopicName",
tokenProvider,
TransportType.AmqpWebSockets);
It is actually possible to use ConnectionString directly but then it has to be augmented to use the right protocol.
var sender = new MessageSender("TransportType=AmqpWebSockets;Endpoint=...", "TopicName")
Or the version that allows to embed EntityPath into the ConnectionString
var connectionBuilder = new ServiceBusConnectionStringBuilder("EntityPath=MyTopic;TransportType=AmqpWebSockets;Endpoint=...")
var sender = new MessageSender(connectionBuilder);
I was having the same issue but his worked for me
var clientOptions = new ServiceBusClientOptions();
clientOptions.TransportType = ServiceBusTransportType.AmqpWebSockets;
client = new ServiceBusClient(connectionString, clientOptions);
sender = client.CreateSender(topicName);
// create a batch
using ServiceBusMessageBatch messageBatch = await sender.CreateMessageBatchAsync();

IP instead of localhost in URI

I am currently trying to create a valid link to a function of my API using this LOC:
Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));
This returns the following:
http://localhost:53800/..../user/821105b1
However, as the link should be accessible from the network, I would need something like the following:
http://192.168.0.12:53800/..../user/821105b1
How can I get this result instead of the one with the localhost??
Thanks in advance!
Calling local libraries will only give you the local IP. You can call an external API like whatismyip or checkip to get the external IP. See here...https://stackoverflow.com/a/7838551

Is calling SmtpClient(host,0) equivalent to calling SmtpClient(host)

We're developing an application which uses SMTP; the host is configured in our DB and we want allow an optional port to be specified (so that SSL can be used in future).
Easiest seems to be to set port==0 when reading from the DB but I need to confirm that .NET will treat this exactly the same as not specifying a port at all?
https://msdn.microsoft.com/en-us/library/67w4as51(v=vs.110).aspx
According to the SmtpClient(string, int) constructor documentation:
If port is zero, Port is initialized using the settings in the application or machine configuration files.
As you can see from the SmtpNetworkElement configuration element documentation, this defaults to port 25, exactly the same as when you call the contructor with only a host parameter.
If you don't want that, it's as simple as:
SmtpClient smtpClient;
if (yourConfiguration.SmtpPort.HasValue)
{
smtpClient = new SmtpClient(yourConfiguration.SmtpHost, yourConfiguration.SmtpPort.Value);
}
else
{
smtpClient = new SmtpClient(yourConfiguration.SmtpHost);
}
Another way to confirm is in this link (SmtpClient source code) (line 160)
if(port == 0)
port = defaultPort;//25

How to create connection string that independent on instance and server name?

i want my application can run on every device that maybe have different server name and instance. my connection string is below:
DBDataContext db = new DBDataContext(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\App_data\DB.mdf;Integrated Security=True;User Instance=False");
If I understand you correctly and the Data Source is always on the local server, why don't you get the ip address of the server with System.Web.HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"];
string myLocalserverIP = System.Web.HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"];
then use that in your Data Source string.

Dynamically switch WCF Web Service Reference URL path through config file

How do you dynamically switch WCF Web Service Reference URL path through config file ?
Are you just wanting to override the URL that is in the config to a different url. Say you have a test service and a live service. You can just do this.
client.Endpoint.Address = new EndpointAddress(Server.IsLiveServer() ?
#"LiveUrl" : #"TestURl");
Where those url come from wherever you want
Just to expand on the answer from Erin: -
MyClient client = new MyService.MyClient();
client.Endpoint.Address = new EndpointAddress(new Uri("insert new url here"),
client.Endpoint.Address.Identity, client.Endpoint.Address.Headers);
client.Open();
HTH!
There is no dynamic switching. Each time you want to use another URL you must create new instance of service proxy (client) and pass EndpointAddress or enpoint configuration name to the constructor.
I have been trying to do the same thing but most of the accepted answers in various posts just change the address. Currently under .net 4.7, simply changing the address itself does not work. If you have two different servers and wanting it to switch from one to the other, you have to do this:
var client = new MyService.Service1Client();
var newAdrEndpoint = new EndpointAddress(new Uri("second server address"));
client = new MyService.Service1Client(client.Endpoint.Binding, newAdrEndpoint);
Essentially you need to create a new service using the same binding from the first server and passing in the new address. This is the simplest method I have found.
sure you can do this, have a look here: How to config clients for a wcf service?
it is absolutely normal to point to localhost in development and to change the address (url) in production in the web.config
you canĀ“t chance endpoint url after any calling.
E.G.
in that case, you will get answer from NEWURL:
MyClient client = new MyService.MyClient();
client.Endpoint.Address = new EndpointAddress("NEWURL");
client.Hello(); //return is hello response from NEWURL
but if you will call any method before changing url, the url will be used from app.config, like next example:
MyClient client = new MyService.MyClient();
client.Endpoint.Address = new EndpointAddress("NEWURL");
client.Hello(); //return is hello response from BASEURL

Categories