I have a question about the best way to design / redesign my software. I have a program that uses a web services to perform certain tasks. Each task is a different set of request / response classes on the server end and a general function call from the Main Hook. EG.
RequestObject req = new RequestObject("SomeVal");
ResponseObject rsp = new ResponseObject();
rsp = WebServiceHook.Method1(RequestObject); //Returns ResponseObject
Each Method takes a different request object and returns a different type of response object.
As it stands i have a class for each of these methods, Each class has a public method Process() that does the interaction. I am trying to determine the best way to group all this code together using OO techniques without sacrificing functionality. I would like just one ProcessMethod in one class that will handle the interaction with the webservice for all the different web methods. So i would only call one ProcessMethod passing a switch of sorts that would define the relevant types and uniques strings that method requires. Some Sample code below:
privatestatic InfoRequest infoReq = null; //TYPES WILL DIFFER IN EACH PROCESS METHOD
privatestatic InfoResponse infoRsp = null; //TYPES WILL DIFFER IN EACH PROCESS METHOD//PARAM TYPES WILL DIFFER IN EACH PROCESS METHODpublicstaticbool Process(INTERFACEREQUEST COMReq, out INTERFACERESPONSE COMRsp)
bool blnReturnVal = false; //Always the same
CInfoRsp InfoRsp = new CInfoRsp(); //TYPES WILL DIFFER IN EACH PROCESS METHOD
CInfoReq InfoReq = (CInfoReq)COMReq; //TYPES WILL DIFFER IN EACH PROCESS METHOD
Globals.dtStart = DateTime.Now; //Always the same
Globals.Log(Logger.LogLevel.Minimum, Logger.LogType.APIMethodEntryPoint, "SOME TEXT HERE", "AND HERE", InfoReq.SalePointID);//Always the same - Except for the Piece of Texttry
Globals.TheService.Url = Settings.URL; //Always the same
infoReq = new InfoRequest(); //TYPES WILL DIFFER IN EACH PROCESS METHOD
infoRsp = new InfoResponse(); //TYPES WILL DIFFER IN EACH PROCESS METHOD
SetRequest(InfoReq); //Set the PHS Request Object = to Our Values Passed from the COM Component
CallWEBServiceMethod(); //Call the Web Service passing the appropriate arguments
SetCOMResponseValues(InfoRsp); //Get the Info Response and Map it to Our InfoResponse Object
COMRsp = InfoRsp; //Assign the Out Variable for COM
blnReturnVal = AssignCOMResponse(InfoRsp); // Set the Return Value
catch (Exception ee) //Catch any Exception that might occur along the Way
//Ensure we set the Out Variable to NULL and the return Value to False, Log this Exception Too
COMRsp = new CInfoRsp(ee);
blnReturnVal = false;
Globals.Log(Logger.LogLevel.All, Logger.LogType.APIMethodException, ee.ToString() + " " + ee.InnerException);
InfoRsp = null;
InfoReq = null;
infoReq = null;
infoRsp = null;
Is what i am trying to achieve possible or am i trying to over engineer this solution. It currently works i just find every time i have to add a new web method call i clone one of the existing classes and change the types in the processMethod and anywhere else that's needed. This is quite tedious and prone to bugs.
The factory would return a class that derives from this abstract base-class, with each class having it's own implementation for the Process method. The app would only have to call the method, without knowing specific details on the class.
Varying the parameters can be done in a similar way; EventArgs is a nice example on how to vary parameters, and still have some control. Imagine a base-class for the arguments, called "BaseProcessArgs"; if you'd need to pass a string and an int, your "Args" class would look like below;
Question about my design so far, I am wondering if i am going about this the right way?
I have a COM Library created in C#, with COMVisible set to true. This library talks to a 3rd party WebService calling various methods depending on the task at hand. For each Request / Response class exposed by the 3rd party DLL I have a Request / Response Pair (Class and Interface) to Marshal variables specifically for a VC6 application.
So with that in my i have added a C# project to the solution to test this code, see test method below:
staticvoid Main(string args)
ICOMReq iReq = new CCOMReq();
ICOMRsp iRsp = new CCOMRsp();
Link l = new Link();
iReq.Date_Time = DateTime.UtcNow;
iReq.Var2 = "1023758145865";
l.DoMethod1(iReq, out iRsp);
//Handle COM Response here
The link class looks like so at the moment be but will have all the methods created once the design is correct:
publicbool DoMethod1(ICOMReq _COMReq, out ICOMRsp COMRsp)
Method1 WebServiceMethod = new Method1(new Method1Request(), new Method1Response(), (CCOMReq)_COMReq, new CCOMRsp());
//Just test code at the moment
COMRsp = null;
Each DoMethod(N) that will be in the link class will look the same performing its task with identical code to the other DoMethods. The key differences between the methods is the Param Types Passed in and the Method1 (Method1Request/Method1Response) type will vary depending on the webmethod to be called.
Class Method1 (There will be one of these for every method I need to implement on the WebService) looks like so:
publicclass Method1 : WebServiceInterfaceBridge<Method1Request, Method1Response, ICOMReq, ICOMRsp>
public Method1(Method1Request WEB_Req, Method1Response WEB_Rsp, CCOMReq COM_Req, ICOMRsp COM_Rsp)
: base(WEB_Req, WEB_Rsp, CBE_Req, CBE_Rsp)
WEB_Req.SOME_DATE_TIME = COM_Req.Date_Time;
//There will be additional methods here in the end as well as a mapping method to convert the WEBMethodResponse to a COMResponse and pass it back to the caller
The class (WebServiceInterfaceBridge) that all Method(N) classes will inherit from is shown below:
publicabstractclass WebServiceInterfaceBridge <T,U,V,W> : WebServiceInterface
where T : classwhere U : classwhere V : classwhere W : class
protected T WebMethod_Request;
protected U WebMethod_Response;
protected V COM_Request;
protected W COM_Response;
public WebServiceInterfaceBridge(T tPHSReq, U uPHSRsp, V vCBEReq, W wCBERsp)
WebMethod_Request = tPHSReq;
WebMethod_Response = uPHSRsp;
COM_Request = vCBEReq;
COM_Response = wCBERsp;
WebMethod_Response = CallWebMethod<T, U>(WebMethod_Request);
And finally here is the class that actually calls the Web service that the WebServiceBridge Class inherits from:
I have used this sample as reference How to use a web cam in C# with .NET Framework 4.0 and Microsoft Expression Encoder 4[^]
and managed to modify the files to stream to my IIS . I have no issue streaming towards my own IP address . However , problem arises when i try to stream it to another IP address . It disconnected after few minutes of streaming but strangely at times it works fine(Not most of the time) . Asked at Microsoft Expression Encoder Forum and no one dares to answer me . I hope here are brave programmers here which can help me solve this particular issue . Do request me if you need to take a look at my codes .
What does this have to do with C#?? A dropped connection doesn't have anything to do with your code. Once the connection starts it's up to Encoder and the client to keep it going. Your code has nothing to do with it.
fisrt COM component loaded fine in C#.net exe framework. My problem is to load another COM component into same C#.net exe framework, and this second COM component will be called from first COM component.
I need to use a generic collection of keyvalue pair. I need thr folloeing:
1) should be generic because my values are a customized class
2) iterating should keep the order in which I added Items
3) I should be able to find the index of the element
4) I need access via Key
A dictionary is ok for 1,2,4 but not for 3
SortedList and SortedDictionary are ok for 1, 3, 4 but not for 2.
If I create a
it is ok for 1,2,3 but not for 4 ( I mean not directly)
//Dictionary<string, double> d = new Dictionary<string, double>();
SortedList<string, double> d = new SortedList<string, double>();
foreach (KeyValuePair<string,double> k in d)
Console.WriteLine(k.Key + " " + k.Value);
// output: a 2 b 4 c 3 z 1// but i need to preserve the order in whicth I added.// I need output: z 1 a 2 c 3 b 4// using Dictionary I preserve the order but how can select item using an index?
KeyValuePair<string, double> i_item = d.ElementAt(2); // if d is a Dictionary it is not possible
Why do you need to be able to get the index of a KeyValuePair if you already got the key?
Maybe take a look at the hint from Keith.
Note: As I just saw after a bit of researching, the Dictionary uses some hashing of the keys to create some order which means it does not necessarily return the items in the order in which they were added
For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair<tkey, tvalue=""> structure representing a value and its key. The order in which the items are returned is undefined.