相关疑难解决方法(0)

WCF命名管道最小示例

我正在寻找WCF命名管道的最小示例(我期望两个最小的应用程序,服务器和客户端,它们可以通过命名管道进行通信.)

Microsoft有一篇很好的文章" 入门教程",它通过HTTP描述了WCF,我正在寻找类似于WCF和命名管道的东西.

我在互联网上发现了几个帖子,但它们有点"先进".我需要一些最小的,只有强制性的功能,所以我可以添加我的代码并使应用程序正常工作.

如何替换它以使用命名管道?

<endpoint address="http://localhost:8000/ServiceModelSamples/Service/CalculatorService"
    binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICalculator"
    contract="ICalculator" name="WSHttpBinding_ICalculator">
    <identity>
        <userPrincipalName value="OlegPc\Oleg" />
    </identity>
</endpoint>
Run Code Online (Sandbox Code Playgroud)

如何替换它以使用命名管道?

// Step 1 of the address configuration procedure: Create a URI to serve as the base address.
Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");

// Step 2 of the hosting procedure: Create ServiceHost
ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);

try
{
    // Step 3 of the hosting procedure: Add a service endpoint.
    selfHost.AddServiceEndpoint(
        typeof(ICalculator),
        new WSHttpBinding(),
        "CalculatorService");

    // Step 4 of the hosting …
Run Code Online (Sandbox Code Playgroud)

c# wcf named-pipes

89
推荐指数
3
解决办法
10万
查看次数

Task.Factory.StartNew vs new Task

有没有人知道做Task.Factory.StartNewvs new Task后跟调用Start任务之间是否有任何区别.看反射器似乎没有太大的区别.所以也许唯一的区别是Task.Factory.StartNew返回已经启动的任务.它是否正确?

我知道Task.Factory.StartNew并且Task.Run有不同的默认选项,Task.Run是.Net 4.5的首选选项.

c# asynchronous c#-4.0

54
推荐指数
2
解决办法
3万
查看次数

System.IO.Exception:管道已损坏

我有两个.NET应用程序通过命名管道相互通信.第一次通过时一切都很好,但是在发送第一条消息之后,服务器将再次监听,该WaitForConnection()方法抛出一条System.IO.Exception消息管道已损坏.
为什么我在这里得到这个例外?这是我第一次使用管道,但类似的模式在过去使用套接字对我有用.

代码啊!
服务器:

using System.IO.Pipes;

static void main()
{
    var pipe = new NamedPipeServerStream("pipename", PipeDirection.In);
    while (true)
    {
        pipe.Listen();
        string str = new StreamReader(pipe).ReadToEnd();
        Console.Write("{0}", str);
    }
}
Run Code Online (Sandbox Code Playgroud)

客户:

public void sendDownPipe(string str)
{
    using (var pipe = new NamedPipeClientStream(".", "pipename", PipeDirection.Out))
    {
        using (var stream = new StreamWriter(pipe))
        {
            stream.Write(str);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对sendDownPipe的第一次调用让服务器打印我发送的消息就好了,但是当它再循环回来再次监听时,它就会大便.

c# named-pipes .net-3.5

21
推荐指数
2
解决办法
3万
查看次数

提升权限不适用于UseShellExecute = false

我想启动一个具有提升权限但具有隐藏窗口的子进程(实际上是相同的控制台应用程序).

我做下一个:

var info = new ProcessStartInfo(Assembly.GetEntryAssembly().Location)
{
    UseShellExecute = true, // !
    Verb = "runas", 
};

var process = new Process
{
    StartInfo = info
};

process.Start();
Run Code Online (Sandbox Code Playgroud)

这工作:

var identity = new WindowsPrincipal(WindowsIdentity.GetCurrent());
identity.IsInRole(WindowsBuiltInRole.Administrator); // returns true
Run Code Online (Sandbox Code Playgroud)

但是UseShellExecute = true创建了一个新窗口,我也无法重定向输出.

所以我下次做的时候:

var info = new ProcessStartInfo(Assembly.GetEntryAssembly().Location)
{
    RedirectStandardError = true,
    RedirectStandardOutput = true,
    UseShellExecute = false, // !
    Verb = "runas"
};

var process = new Process
{
    EnableRaisingEvents = true,
    StartInfo = info
};

DataReceivedEventHandler actionWrite …
Run Code Online (Sandbox Code Playgroud)

c# uac processstartinfo elevated-privileges

18
推荐指数
2
解决办法
2万
查看次数

C#:GUI,用于显示来自Windows服务的实时消息

我编写了一个C#windows服务,可以将消息写入自定义EventLog或任意数量的文件.这些消息都标记有一些优先级(因此,例如,只有ERROR和WARNING存储在EventLog中,但如果需要,可以将更多内容存储到文件中).

我现在要做的是创建一个GUI,可以监听这些消息并实时显示它们.允许用户观看当前的消息(在任何他们想要的优先级),而不需要的一切存储到文件中.我认为这是某种形式的挂钩到服务的一个单独的程序,但我不能确定从哪里开始.

这是我第一次真正的Windows服务,所以我似乎缺少一些关键字找出如何做到这一点?是否有任何代码样本,教程,参考文献等,为如何做这样的事情?

更新
很多有用的答案,我喜欢它,有很多方法可以解决问题!我想我将实现一个基于WCF的自托管解决方案.我仍然非常关注细节,因为我正在努力了解WCF(我相信它对我来说在其他项目中非常有用)...但到目前为止,我发现这里的视频是最多的有用的介绍方法.

c# windows-services event-log winforms

6
推荐指数
1
解决办法
1万
查看次数

从一个C#控制台应用程序发送消息到另一个

首先,我已经阅读了所有相关主题并且他们给出了一般性的想法,但实现对我不起作用:
将字符串从一个控制台应用程序
发送 到另一个如何将输入发送到控制台,就像用户正在键入一样?
从控制台应用程序(C#/ WinForms)发送输入/获取输出
我有一个控制台应用程序,它在后台执行某些操作,直到请求取消.典型使用场景是:
1)执行应用程序
2)输入输入数据
3)发出启动命令
4)经过一段时间后,输入停止命令
5)退出应用程序
子应用程序Program.cs:

static void Main()
{
    Console.WriteLine("Enter input parameter : ");
    var inputParameter = Console.ReadLine();
    Console.WriteLine("Entered : " + inputParameter);

    var tokenSource = new CancellationTokenSource();
    var token = tokenSource.Token;
    Task.Factory.StartNew(() =>
        {
            while (true)
            {
                if (token.IsCancellationRequested)
                {
                    Console.WriteLine("Stopping actions");
                    return;
                }
                // Simulating some actions
                Console.Write("*");
            }
        }, token);
    if (Console.ReadKey().KeyChar == 'c')
    {
        tokenSource.Cancel();
        Console.WriteLine("Stop command");
    }
    Console.WriteLine("Finished");
    Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

我正在寻找的是某种控制该应用程序的主机实用程序 - 在每个实例上生成多个实例并执行所需的用户操作.主持人申请Program.cs …

c# console interop message process

4
推荐指数
1
解决办法
1万
查看次数

优雅地关闭命名管道并处理流

我有一个双向命名管道。我不确定如何优雅地关闭它,但是,一旦完成,我会关闭-如果我从客户端关闭连接,则服务器端在尝试处置StreamReader和StreamWriter时会抛出异常。米使用。我目前正在抓住它,但是对我来说这似乎是一项艰巨的工作。

服务器端代码:

Thread pipeServer = new Thread(ServerThread);
pipeServer.Start();

private void ServerThread(object data)
{
    int threadId = Thread.CurrentThread.ManagedThreadId;
    log.Debug("Spawned thread " + threadId);

    PipeSecurity ps = new PipeSecurity();
    SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
    ps.AddAccessRule(new PipeAccessRule(sid, PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow));
    ps.AddAccessRule(new PipeAccessRule(WindowsIdentity.GetCurrent().Owner, PipeAccessRights.FullControl, System.Security.AccessControl.AccessControlType.Allow));
    log.Debug("Pipe security settings set [Thread " + threadId + "]");

    NamedPipeServerStream pipeServer =
        new NamedPipeServerStream("RDPCommunicationPipe", PipeDirection.InOut, numThreads, PipeTransmissionMode.Message, PipeOptions.None, 0x1000, 0x1000, ps);

    log.Debug("Pipe Servers created");

    // Wait for a client to connect
    log.Info("Pipe created on thread " + threadId …
Run Code Online (Sandbox Code Playgroud)

c# named-pipes

3
推荐指数
1
解决办法
3299
查看次数

c#通知正在运行的进程

是否有可能以某种方式从"外部"通知正在运行的进程?

我有一个理论上永远存在的C#程序.我想通知它手动触发一些动作.如果在Windows和Linux(单声道)上可以使用此解决方案,那将是最好的.

编辑:

  1. 解决方案应该没有用户界面
  2. 我的程序目前是Web服务的一部分.在初始化时,会创建一个新的Theread,它使用Task该类来保持活跃状态

c# process

0
推荐指数
1
解决办法
519
查看次数

使用malloc在父母和孩子之间进行通信

如果我从malloc请求int的内存大小,我从一个父级创建'n'子进程.每个子进程更新(添加一个)内存中的值是否可行,所以到底父进程读取值?

c malloc fork

0
推荐指数
1
解决办法
98
查看次数