WCF - Object losing it's values when passed to WCF Service - c#

I am new to using WCF Services but have began writing a new project and have ran into this problem. I am also new to programming, so forgive me if wording is incorrect!
I have an object called Profile that I can retrieve on my client from WCF, and that works without problems - using a GetProfile method.
I then wanted to make modifications to the object on the client side and send it back through the WCF service using an UpdateProfile method. At this point, the object always seems to be reinstantiated (therefore overwriting all values to null).
Any help is appreciated.
So the GetProfile method works beautifully.
Make changes to the RAIS_Profile on my client works beautifully
Send the RAIS_Profile back using UpdateProfile and every value in the object is blank once in that method.
Public Function GetProfile(ByVal DocumentNumber As Integer) As RAIS_Profile Implements IRAISAPI.GetProfile
Dim Doc As New RAIS_Profile
'blah
'blah
Return Doc
End Function
Public Function UpdateProfile(ByVal pDoc As RAIS_Profile) As String Implements IRAISAPI.UpdateProfile
Return pDoc.DocumentNumber
End Function
Any help is appreciated!

Look at the service contracts you have, both on client and server. If the parameters aren't precisely named the same way, then you'll encounter this kind of behaviour.
See Here

Related

How can I connect my front end to my individually created backend

I am a beginner in creating a code and I am trying to code using vue js for my front end and aspnet core for my backend (web api). There arent many references in creating a to do list with this two connected.
I have already individually set up my front end in vs code while my back end in c#. I am having a hard time in connecting this two as there is not much update reference. How can I call my backend and connect it to my vue js.
thank you!
The first thing you need to do is open the back end CORS settings.
It connects to an endpoint on the Vue side, you can use axios framework to pull data
Here is an example
https://www.freecodecamp.org/news/how-to-build-an-spa-with-vuejs-and-c-using-net-core/
All you need to do is call the backend via API. Get the data via GET method, send the data via POST method. Let's say you want to get person list from the backend. You can do this -
const response = await fetch('https://yourawesomebackend.com/api/person/getpeople');
const content = await response.json();
// Now manipulate the DOM with the response.
In your backend, you might want to do this -
[Route("api/[controller]/[action]")]
[ApiController]
public class PersonController : ControllerBase
{
public IActionResult GetPeople()
{
// read people from database
var people = getFromDatabase();
return Ok(people);
}
}
Hope this helps.

Any downsides to replacing REST endpoints with SignalR?

I'm building a fairly simple single page app. It's basically a list of items, where each item has some details, an activity log, and a current status along with some buttons to trigger actions on the server to advance the status along a workflow.
It was originally written using MVC and REST/Web API but I got stuck on the problem of keeping concurrent users up to date. For example, if User A adds an item, we want the list on User B's screen to now update to include it.
To solve this I looked into SignalR which works great. But I had a problem.
When adding an item (using POST) the callback adds the item on the requesting client. This is fine.
I then triggered a SignalR broadcast on the server to tell all clients about the new item. This worked fine except the local client, who now has 2 items.
I was looking into filtering the duplicate id client-side, or sending the connection id with the POST, then broadcast to all clients except the requester but it seems a bit needlessly complicated.
Instead I'm just doing this.
public class UpdateHub : Hub
{
public void AddNewItem(NewItem item)
{
// and some server-side stuff, persist in the data store, etc
item.trackingID = new Guid();
item.addLogEntry("new item");
// ...
dataStore.addItem(item);
// send message type and data payload
Clients.All.broadcastMessage("add", item);
}
}
It seems a lot simpler to just get rid of all the REST stuff altogether, so am I missing anything important?
It'll run on an intranet for a handful of users using IE11+ and I guess we do lose some commonly-understood semantics around HTTP response codes for error handling, but I don't think that's a huge deal in this situation.
In order to solve duplicate you can try to use Clients.Others inside Hub class, or AllExcept(id) if you not in the Hub class.
Clients.Others.broadcastMessage("add", item);
In your case using SignalR shouldn`t have any downsides.

Rest to make call to another process just once in entire life cycle

say Rest service got one request which is giving a call to another service (email service),
but when Rest service got another request which is giving a call to email service I want it to skip that step.
also rest service should be intelligent enough to see if email process is being running or stopped if stopped any next request to rest service is allowed to call email service.
Rest Method : -
void SaveDataAndCallEmailService(Data data)
{
cntx.Add(data);
cntx.SaveChanges();
SendEmail();
}
now my query is how to make "SendMail()" method to be called only once only if Email Process is not running.
for e.g. : -
void SaveDataAndCallEmailService(Data data)
{
cntx.Add(data);
cntx.SaveChanges();
if(!EmailProcessIsRunning)
SendEmail();
EmailProcessIsRunning= true;
}
Edit: -
Now, here "EmailProcessIsRunning" is always false, even though I marked it as true.
When server receive second request I found "EmailProcessIsRunning" is false again.
How to persist it's value?
In your code sample EmailProcessIsRunning is not defined anywhere. Makes if more difficult to see if there's a scope issue.
Assuming there's no scope issues, if you want to persist the value saved to EmailProcessIsRunning between requests of on a stateless webapi server you'll need to add a DependecyResolver to your server. Read this for more information http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver
edit: (a simpler answer)
You can add a static property to your class, and set it to true when the process is started.
e.g.
public bool static EmailProcessIsRunning { get; set; }

Integer value is lost in web service call

I have an application that makes a web service call to get the URL of an MSI depending on whether the user's computer is 32bit or 64bit.
The call GetURLByOS takes 2 methods (1. string AuthenticationInfo , 2. int osBit). As I'm debugging, I can see the authentication info. The osBit's value is 8 (for 64bit) when calling into the web service. But its value is lost (0) when actually in the web service.
Can someone help me figure out why the integer value is lost?
Update:
I'm attaching to the process. In the client, I see value 8 being passed in. In the web service call, I see 0.
This is a SOAP web service call.
Here's the WSDL code on the client:
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://mydomain.com/product/1.0/GetURLByOs", RequestNamespace = "http://mydomain.com/product/1.0", ResponseNamespace = "http://mydomain/product/1.0", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public string GetURLByOs(string eTicket, int OsBitType)
{
object[] results = this.Invoke("GetURLByOs", new object[] {
eTicket, OsBitType});
return ((string)(results[0]));
}
Here's the actual web service:
[WebMethod]
public string GetURLByOs(string eTicket, int osBitType)
{
return MyFacade.GetUrl(eTicket, osBitType);
}
BTW, when I change the parameter to type string, it gets passed properly (value "8"). It's only when I pass it as an integer that the value is zeroed out.
I found out what was the problem. On the (client) WSDL code, the parameter is OsBitType. But on the actual web service, the parameter is osBitType. After changing the web service parameter to OsBitType, it's working fine.
Strange thing is, this doesn't occur if the parameter is a string.
For my situation, after update reference of web service, it's working fine.

Client side message functionality

How the ilusion of causing site to change content from server side is done? Let the example be gmail chat or chat on facebook. Or even new message sign on stack overflow.
Is it done by http://en.wikipedia.org/wiki/Comet_(programming)) ?
Thanks for help
That sort of things is usually done with a block of JavaScript firing again and again according to a timer. It will check the state of the things in the database and adjust something in the markup. For instance, change the CSS class of some element to introduce a different color or a bold font, replace a picture with the one done in a brighter color etc. Quite simple really. No magic involved.
The client side has to 'poll' the server for changes. i.e. a timer based Ajax call that checks the server every 15 seconds for new data, and takes action based on the result.
very loose example:
setTimeout('checkMessages()',15000);
function checkMessages() {
//using jquery
$.get( .......... , function (data) { if (data == "newmsg") { $('#newmsgind').blink(); });
setTimeout('checkMessages()',15000);
}
Web browsers don't really maintain a connection to the server. You pull a page and that's it. Ajax allows continuous asynchronous communication, but it's always the client that initiates.
If you really don't like the javascript approach, you can write a Java applet that works the way you seem to prefer, maintaining an open connection to the server. But that's a heavyweight solution to what is usually a lightweight problem.

Categories