افزودن خودکار کلاس های WebAPI و SignalR Hub به برنامه در حالت SelfHost
نویسنده: ابوالفضل رجب پور
تاریخ: ۱۳۹۳/۰۸/۰۵ ۱۳:۵۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
public class MessageHub : Hub
{
public void NotifyAllClients()
{
Clients.All.Notify();
}
} static void Main(string[] args)
{
const string baseAddress = "http://localhost:9000/"; // "http://*:9000/";
using (var webapp = WebApp.Start<Startup>(baseAddress))
{
Console.WriteLine("Start app...");
var hubConnection = new HubConnection(baseAddress);
IHubProxy messageHubProxy = hubConnection.CreateHubProxy("messageHub");
messageHubProxy.On("notify", () =>
{
Console.WriteLine();
Console.WriteLine("Notified!");
});
hubConnection.Start().Wait();
Console.WriteLine("Start signalr...");
bool dontExit = true;
while (dontExit)
{
var key = Console.ReadKey();
if (key.Key == ConsoleKey.Escape) dontExit = false;
messageHubProxy.Invoke("NotifyAllClients");
}
}
} public partial class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
var hubConfiguration = new HubConfiguration()
{
EnableDetailedErrors = true
};
appBuilder.MapSignalR(hubConfiguration);
appBuilder.UseCors(CorsOptions.AllowAll);
}
} {"StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:\r\n{\r\n Date: Mon, 27 Oct 2014 09:36:48 GMT\r\n Server: Microsoft-HTTPAPI/2.0\r\n Content-Length: 0\r\n}"} AppDomain.CurrentDomain.Load(typeof(MessageHub).Assembly.FullName);
class LoadAssemblyHelper
{
public static void Load(string searchPattern)
{
var path = Assembly.GetExecutingAssembly().Location;
var entityAssemblies = Directory.GetFiles(Path.GetDirectoryName(path), searchPattern: searchPattern);
var assemblyNames = entityAssemblies.Select(e => AssemblyName.GetAssemblyName(e)).ToList();
assemblyNames.ToList().ForEach(e => AppDomain.CurrentDomain.Load(e));
}
} static void Main(string[] args)
{
//AppDomain.CurrentDomain.Load(typeof(MessageHub).Assembly.FullName);
//AppDomain.CurrentDomain.Load(typeof(MessageController).Assembly.FullName);
LoadAssemblyHelper.Load("myFramework.*.dll");
const string baseAddress = "http://*:9000/";
using (var webapp = WebApp.Start<Startup>(baseAddress))
{
...
}
}