简体   繁体   中英

Communication from a C# application to an ASP.NET application

I'm creating a host application (C#) for a ASP.NET application. What I'd like to accomplish is having the ASP application getting events from the C# application. With this data I need to call a server side function with the HTTP Context/session from the ASP client side. It's possible to just refresh the page with a querystring.

I currently have two approaches:

1) C# WCF with net.tcp to ASP 通讯概述-从ASP.NET的net.tcp C# Issue: Session (HTTP context) is NULL when directly contacting the server side layer. I can update static variables and so on, but I'm not able to tell the client side to refresh/navigate with a querystring (so I can get the context at next page_load).

2) C# WebBrowser.Document.InvokeScript(myJavascript()) C#WebBrowser.Invoke到JS

Issue: I'm not able to reach static variables (they're for some reason NULL) when I reach the server side on the ASP application. I've tried using Session["MyVariable"] = "test"; in a page_load, invoke the javascript function and check the session variable, but this is NULL.

Any help or pointings to the right direction would be very helpful. If needed I'll attach some code examples.

Thanks!

Edit 1: From my WebBrowser all I can see is the Default.aspx page, so when creating javascript function from issue #2, I need to create this in this file. If not I'm not able to invoke it.

For Issue 1, did you tried to enable asp.net compatibility?

Services running in ASP.NET Compatibility mode participate fully in the ASP.NET application pipeline and can make use of ASP.NET features such as file/URL authorization, session state, and the HttpContext class.

Here's a couple of links:

WCF Services and ASP.NET

ASP.NET Compatibility

You can decorate your service implementation with AspNetCompatibilityRequirementsMode.Required to explicitly suggest WCF that those implementation must require that in the web.config the asp.net compatibility is enabled, otherwise you can leave AspNetCompatibilityRequirementsMode.Allowed to doesn't enforce it, but still be able to read HttpContext if the asp.net comp is enabled.

on the service implementation:

[AspNetCompatibilityRequirements(RequirementsMode =
                   AspNetCompatibilityRequirementsMode.Required)]

under system.serviceModel:

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

For issue 1, I believe you are trying to "instance" the ASP application class from the WCF service. Like loading the DLL on your own or something that way. Do not do it - it will always make the statics null and sessions null. You need a invert the control here. Create a callback and this thing could work like a charm.

Why dont you look at options like SSE (Server Sent Events) ? Probably that can solve the issue you have with your point 2.

Post your code if possible.

I'm wondering if you can't use SignalR here.

Assuming it can be fitted in your solution. Your C# app can send data back to your server, through a SignalR or Web API request.

Your server then sends an alert to the client session via SignalR prompting it to refresh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM