小编Ska*_*ami的帖子

SignalR控制台应用示例

是否有使用signalR将消息发送到.net集线器的控制台或winform应用程序的小示例?我已经尝试过.net示例并查看了wiki但是对我来说没有意义的中心(.net)和客户端(控制台应用程序)之间的关系(找不到这个例子).应用程序是否只需要连接集线器的地址和名称?

如果有人可以提供一小段代码,显示应用程序连接到集线器并发送"Hello World"或.net中心收到的内容?

PS.我有一个标准的集线器聊天示例,如果我尝试在Cs中为它分配一个集线器名称,它停止工作,即[HubName("test")],你知道这个的原因吗?

谢谢.

当前控制台应用代码.

static void Main(string[] args)
{
    //Set connection
    var connection = new HubConnection("http://localhost:41627/");
    //Make proxy to hub based on hub name on server
    var myHub = connection.CreateProxy("chat");
    //Start connection
    connection.Start().ContinueWith(task =>
    {
        if (task.IsFaulted)
        {
            Console.WriteLine("There was an error opening the connection:{0}", task.Exception.GetBaseException());
        }
        else
        {
            Console.WriteLine("Connected");
        }
    }).Wait();

    //connection.StateChanged += connection_StateChanged;

    myHub.Invoke("Send", "HELLO World ").ContinueWith(task => {
        if(task.IsFaulted)
        {
            Console.WriteLine("There was an error calling send: {0}",task.Exception.GetBaseException());
        }
        else
        {
            Console.WriteLine("Send Complete.");
        }
    });
 } …
Run Code Online (Sandbox Code Playgroud)

c# signalr

77
推荐指数
5
解决办法
9万
查看次数

SignalR无法读取未定义的属性客户端

我正在尝试将SignalR添加到我的项目中(ASPNET MVC 4).但我不能让它发挥作用.

在下图中,您可以看到我收到的错误. 图片 我已经阅读了很多stackoverflow帖子,但没有一个是解决我的问题.

这是我到目前为止所做的:

1) Ran Install-Package Microsoft.AspNet.SignalR -Pre
2) Added RouteTable.Routes.MapHubs(); in Global.asax.cs Application_Start()
3) If I go to http://localhost:9096/Gdp.IServer.Web/signalr/hubs I can see the file content
4) Added <modules runAllManagedModulesForAllRequests="true"/> to Web.Config
5) Created folder Hubs in the root of the MVC application
6) Moved jquery and signalR scripts to /Scripts/lib folder (I'm not using jquery 1.6.4, I'm using the latest)
Run Code Online (Sandbox Code Playgroud)

这是我的Index.cshtml

<h2>List of Messages</h2>

<div class="container">
    <input type="text" id="message" />
    <input type="button" id="sendmessage" value="Send" />
    <input …
Run Code Online (Sandbox Code Playgroud)

c# signalr signalr-hub signalr.client

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

RabbitMQ异步支持

RabbitMQ .NET客户端是否有任何异步支持?我希望能够异步连接和使用消息,但到目前为止还没有找到办法.

(对于消费消息,我可以使用EventingBasicConsumer,但这不是一个完整的解决方案.)

为了给出一些上下文,这是我现在如何使用RabbitMQ的一个例子(代码取自我的博客):

var factory = new ConnectionFactory() { HostName = "localhost" };

using (var connection = factory.CreateConnection())
{
    using (var channel = connection.CreateModel())
    {
        channel.QueueDeclare("testqueue", true, false, false, null);

        var consumer = new EventingBasicConsumer(channel);
        consumer.Received += Consumer_Received;
        channel.BasicConsume("testqueue", true, consumer);

        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# asynchronous rabbitmq

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

OWIN + SignalR + Autofac

摘自:http://docs.autofac.org/en/latest/integration/signalr.html:

"OWIN集成中的常见错误是使用GlobalHost.在OWIN中,您可以从头开始创建配置.在使用OWIN集成时,不应该在任何地方引用GlobalHost."

这听起来很合理.但是,如何IHubContext通过ApiController解析,就像通常的(非OWIN):

GlobalHost.ConnectionManager.GetHubContext<MyHub>()

我无法在任何地方找到这个参考,我现在唯一的方法是HubConfiguration在同一个容器中注册实例并执行此操作:

public MyApiController : ApiController {
  public HubConfiguration HubConfig { get; set; } // Dependency injected by
                                                  // PropertiesAutowired()

  public IHubContext MyHubContext { 
    get { 
      return HubConfig
        .Resolver
        .Resolve<IConnectionManager>()
        .GetHubContext<MyHub>(); 
     } 
  }

  // ...

}
Run Code Online (Sandbox Code Playgroud)

但是,这对我来说似乎相当冗长.这样做的正确方法是什么?更具体一点,是否有一种干净的注册方式IConnectionManager

编辑:

我最终做的是:

var container = builder.Build();
hubConfig.Resolver = new AutofacDependencyResolver(container); 

app.MapSignalR("/signalr", hubConfig);

var builder2 = new ContainerBuilder();
builder2
  .Register(ctx => hubConfig.Resolver.Resolve<IConnectionManager>())
  .As<IConnectionManager>();

builder2.Update(container);
Run Code Online (Sandbox Code Playgroud)

但我觉得必须有一种更简单的方法来IConnectionManager注入控制器.

c# autofac signalr owin

14
推荐指数
2
解决办法
4694
查看次数

如何在log4net中注册自定义IObjectRenderer?

我正在研究使用log4net,我发现IObjectRenderer接口很有趣.它将允许我们控制类型的记录方式,并提供不同的,可能更加用户友好的ToString()实现.我刚刚开始查看log4net,似乎找不到以编程方式设置类型和渲染器之间关联的逻辑方法.

我发现这可以通过阅读手册在XML配置文件中设置,但它没有给我任何关于以编程方式添加这些内容的提示.在我看来,在某些情况下你宁愿使用程序化对象渲染器,所以我很好奇如何做到这一点.

c# log4net

9
推荐指数
1
解决办法
3395
查看次数

在调用Initialize后,如何在AutoMapper中添加映射?

我有几个共享映射代码的ASP.Net应用程序,所以我创建了一个通用的automapper init类.

但是,在我的一个应用程序中,我有一些特定的类要添加到配置中.

我有以下代码:

public class AutoMapperMappings
{
    public static void Init()
    {
        AutoMapper.Mapper.Initialize(cfg =>
        {
            ... A whole bunch of mappings here ...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

// Call into the global mapping class
AutoMapperMappings.Init();

// This erases everything
AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<CustomerModel, CustomerInfoModel>());
Run Code Online (Sandbox Code Playgroud)

如何在不破坏已初始化的内容的情况下添加此唯一映射?

c# automapper

9
推荐指数
1
解决办法
8209
查看次数

使用OWIN静态文件时配置客户端缓存

这是我的Startup.cs,我将索引页面映射到路径'/ app'.

...
using Microsoft.Owin.FileSystems;
using Microsoft.Owin.StaticFiles;
using Microsoft.Owin.Diagnostics;
[assembly: OwinStartup(typeof(conApi.Startup))]

namespace conApi
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ////Set static files
            ConfigureFiles(app);

            //Enable Cors
            app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        }

        public void ConfigureFiles(IAppBuilder app)
        {
            app.Map("/app", spa =>
            {
                spa.Use((context, next) =>
                {
                    context.Request.Path = new PathString("/index.html");

                    return next();
                });

                spa.UseStaticFiles();
            });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它就像一个魅力,但我不知道如何配置客户端缓存.我想知道如果在使用OWIN静态文件时可以设置Expires标头?

解决方案
Tratcher提供了StaticFilesOptions类文档等的链接,这些链接引导我找到解决方案.将StaticFilesOptions添加到ConfigureFiles方法,如下所示:

public void ConfigureFiles(IAppBuilder app)
{
    var staticFileOptions = new StaticFileOptions
    {
        OnPrepareResponse = (StaticFileResponseContext) =>
        {
            StaticFileResponseContext.OwinContext.Response.Headers.Add("Cache-Control",new[] { "public", "max-age=1000" }); …
Run Code Online (Sandbox Code Playgroud)

c# caching owin

7
推荐指数
1
解决办法
1165
查看次数

.Net Core 3.0 System.Text.Json 以最小日期时间和 UTC 偏移量失败

我一直在从 2.2 迁移到 3.0,到目前为止一切都非常顺利,但是当用新的 System.Text.Json 替换 Newtonsoft 时,我遇到了以下问题。

尝试使用 UTC 偏移量反序列化 DateTime 最小值时,会引发以下异常

未处理的异常。System.Text.Json.JsonException:无法将 JSON 值转换为 System.DateTime。路径:$.DateCreated | 行号:2 | 字节位置内联:43

例子:

using System;
using Newtonsoft.Json;      

class Program
{
    static void Main(string[] args)
    {
        var json = "{\"MyDateTime\": \"0001-01-01T00:00:00+01:00\"}";
                
        var newtonDeserialized = JsonConvert.DeserializeObject<Class>(json);

        //Bad things happen here.
        var systemDeserialized =  System.Text.Json.JsonSerializer.Deserialize<Class>(json);
        
        Console.WriteLine(newtonDeserialized.MyDateTime);
        Console.WriteLine(systemDeserialized.MyDateTime);
    }
}

class Class
{
    public DateTime MyDateTime {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

在线示例可以在这里找到Fiddle

我想我可以编写自己的 DateTime 序列化程序,但必须有一种更简单/更简洁的方法来完成 Newtonsoft 所做的工作而无需任何配置。

这发生在3.0.1003.1.500并且5.0.100

c# datetime json .net-core-3.0 system.text.json

7
推荐指数
0
解决办法
2008
查看次数

在XNA中使用RenderTarget2D时颜色错误

我已经开始使用XNA制作基于2D精灵的Windows游戏.我对它还不是很有经验,但我正在学习.让我先说我正在使用XNA游戏工作室3.1,我还没有更新到4.0(还).

我想要完成的是能够将所有精灵绘制到固定大小的缓冲区,然后在渲染过程结束时缩放到实际后备缓冲区的大小,然后绘制到该缓冲区.我不确定通常支持多种分辨率,但它似乎是一个适合我的解决方案.

我试图通过使用RenderTarget2D对象来绘制我的所有东西来实现这一点,然后从中获取Texture2D并将其绘制到后台缓冲区.

我的代码看起来像这样:

private RenderTarget2D RenderTarget;
private DepthStencilBuffer DepthStencilBufferRenderTarget;
private DepthStencilBuffer DepthStencilBufferOriginal;
private SpriteBatch SpriteBatch;

protected override void Initialize()
{
  base.Initialize();
  RenderTarget = new RenderTarget2D(GraphicsDevice, 1920, 1080, 1, SurfaceFormat.Single);

  DepthStencilBufferRenderTarget = new DepthStencilBuffer(GraphicsDevice, 
  1920, 1080, GraphicsDevice.DepthStencilBuffer.Format);

  DepthStencilBufferOriginal = GraphicsDevice.DepthStencilBuffer;
  SpriteBatch = new SpriteBatch(GraphicsDevice);
}

protected override void Draw(GameTime gameTime)
{
  GraphicsDevice.DepthStencilBuffer = DepthStencilBufferRenderTarget;
  GraphicsDevice.SetRenderTarget(0, RenderTarget);
  GraphicsDevice.Clear(Color.Black);

  SpriteBatch.Begin();
  //drawing all stuff here
  SpriteBatch.End();

  GraphicsDevice.DepthStencilBuffer = DepthStencilBufferOriginal;
  GraphicsDevice.SetRenderTarget(0, null);
  GraphicsDevice.Clear(Color.Black);
  Texture2D output = RenderTarget.GetTexture();

  SpriteBatch.Begin();
  Rectangle backbuffer = new Rectangle(0, 0, …
Run Code Online (Sandbox Code Playgroud)

c# xna

5
推荐指数
1
解决办法
997
查看次数

具有任意变焦中心的 XNA 2d 相机

我在 XNA 中有一个工作 2D 相机,具有以下功能:

ms = Mouse.GetState();
msv = new Vector2(ms.X, ms.Y);  //screenspace mouse vecor
pos = new Vector2(0, 0); //camera center of view
zoom_center = cursor; //I would like to be able to define the zoom center in world coords
offset = new Vector2(scrnwidth / 2, scrnheight / 2);
transmatrix = Matrix.CreateTranslation(-pos.X, -pos.Y, 0)
    * Matrix.CreateScale(scale, scale, 1)
    * Matrix.CreateTranslation(offset.X, offset.Y, 0);
inverse = Matrix.Invert(transmatrix);
cursor = Vector2.Transform(msv, inverse);  //the mouse position in world coords      
Run Code Online (Sandbox Code Playgroud)

我可以移动相机位置并更改缩放级别(为简洁起见,我没有在此处粘贴其他代码)。相机始终围绕屏幕中心进行缩放,但我希望能够围绕任意缩放点(在这种情况下为光标)进行缩放,例如独立游戏dyson http://www.youtube.com/watch? v=YiwjjCMqnpg&feature=player_detailpage#t=144s …

c# xna camera 2d zooming

5
推荐指数
1
解决办法
3594
查看次数