- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 11814
Mef is Managed Extensibility Framework. That means that with MEF we can easily make plugin system.
To write this article mostly I was using this article.
In this my small example I am going to make one class library, called MefExport, and with that library I will display "hello world" message in console, in separate solution, in order to have separated as much as possible.
Then I will make console application, called MefImport where I will use method from MefExport.
Let's first make console application, which we will call MefExport:
Right click on references -> Add Reference...
Choose System.ComponentModel.Composition
Code for export:
using System; using System.ComponentModel.Composition; namespace MefExport { public interface IHelloWorld { string HelloWorld(); } [Export(typeof(IHelloWorld))] public class MefHelloWorld : IHelloWorld { public string HelloWorld() { return "Hello world"; } } }
After building copy export dll in the lib folder. Change line: catalog.Catalogs.Add(new DirectoryCatalog(@"../../lib")); According to where dll is located.
Import:
using System; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using MefExport; namespace MefImport { class Program { private CompositionContainer _container; [Import(typeof(IHelloWorld))] private IHelloWorld helloWorld; private Program() { var catalog = new AggregateCatalog(); catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly)); catalog.Catalogs.Add(new DirectoryCatalog(@"../../lib")); _container = new CompositionContainer(catalog); try { this._container.ComposeParts(this); } catch (CompositionException compositionException) { Console.WriteLine(compositionException.ToString()); } } static void Main(string[] args) { Program p = new Program(); Console.WriteLine(p.helloWorld.HelloWorld()); Console.ReadKey(); } } }
Example project download from here.
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 6358
First I downloaded Oracle Developer Tools for Visual Studio 2015 from here. In my reference list I added Oracle.ManagedDataAccess. My App.config looks like:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="maxaDB" connectionString="User ID=userName;Password=pass;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=ipAddress)(PORT=myPort))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=XE)))" /> </connectionStrings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
User ID and Password have to go before Data Source. Then I added "Oracle.ManagedDataAccess.Client;" in using list, and after that connecting was simple:
string maxaDb = ConfigurationManager.ConnectionStrings["maxaDB"].ConnectionString; OracleConnection conn = new OracleConnection();In using list you will need System.Configuration, as well in references.
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 6209
In my case I was receiving this error because I was trying to access configuration in my App.config, like:
string maxaDb = ConfigurationManager.ConnectionStrings["maxaDB"].ConnectionString;
To solve this I had to add System.Configuration assembly:
In your using list add:
using System.Configuration;
- Details
- Written by: Stanko Milosev
- Category: C#
- Hits: 6875
Client side is easiest part, javascript, in my case looks something like this:
var ws; window.onload = function () { if ("WebSocket" in window) { // Let us open a web socket ws = new WebSocket("ws://127.0.0.1:9192"); ws.onmessage = function (evt) { Log(evt.data) }; ws.onclose = function() { // websocket is closed. Log("Connection is closed..."); }; } else { // The browser doesn't support WebSocket Log("WebSocket NOT supported by your Browser!"); } } function WebSocketSendMessage() { ws.send(document.getElementsByName("inputSend")[0].value); } function Log(message) { document.getElementsByName("log")[0].innerHTML = document.getElementsByName("log")[0].innerHTML + '<br/>' + message; }
To create new Socket use code:
ws = new WebSocket("ws://127.0.0.1:9192"); - note that here I defined IP address and port, later you will see that in C# code I also defined on which IP address and port server will listen messages.
Event onmessage will be triggered when message is received, and to send message use method send, and that is all.
Simple example of WebSockets in C# I did it with Fleck, you can use NuGet to add it to references:
Create new console application, write code like:
namespace WebSocket { using System; using Fleck; public class Program { static void Main(string[] args) { var server = new WebSocketServer("ws://127.0.0.1:9192"); server.Start(socket => { socket.OnOpen = () => Console.WriteLine("Listening... "); socket.OnClose = () => Console.WriteLine("Closed!"); socket.OnMessage = message => { Console.WriteLine(message); }; }); Console.ReadLine(); } } }
Notice:
var server = new WebSocketServer("ws://127.0.0.1:9192"); - as I wrote before this is IP address and port on which server listens messages.
I implemented OnMessage event which will trigger when message from the client arrives.