Archive for the ‘.NET’ Category

WSDL VS MEX

July 14, 2017

WSDL VS MEX

MEX and WSDL are two different schemes to tell potential clients about the structure of your service. So you can choose to either make your service contracts public as (MEX) or WSDL.

A WSDL is generally exposed through http or https get urls that you can’t really configure (say for security limitations or for backward compatibility). MEX endpoints expose metadata over configurable endpoints, and can use different types of transports, such as TCP or HTTP, and different types of security mechanisms.

Advertisements

Visual Studio remove references count (Codelens)

July 5, 2017

Remove/Set the codelens in VS editor

Tools -> Options -> Text Editor -> All Languages -> CodeLens -> check/uncheck

Restore NuGet Packages

September 28, 2016

If Solution -> Restore NuGet Packages doesn’t work
delete the packages folder and try again and then rebuild solution.
That should fix it.

 

How to use __doPostBack

February 5, 2015

Client

<script type=“text/javascript”>
function SaveWithParameter(parameter){
   __doPostBack(‘btnSave’, parameter)
}
</script>

Server

public void Page_Load(object sender, EventArgs e)
{
string sender = Request[“__EVENTTARGET”]; // btnSave
string eventarg = Request[“__EVENTARGUMENT”]; //parameter
}

Call ASP.NET codebehind from javascript (PageMethod)

November 17, 2014
The first thing to do is to add a ScriptManager to our page,
and tell it to accept page Methods.
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods="True"></asp:ScriptManager>

Next add the codebehind method to call, it must be static and have
the WebMethod attribute

[WebMethod()]
public static void SetDimensions(int width, int height)
{        
    //Method code
}

Finally call the method using PageMethods 

<script type="text/javascript">
    function setDimensions() {
        PageMethods.SetDimensions(width, height);
    }
</script>

WCF service for AJAX

August 6, 2014

WCF service for client AJAX call returning JSON

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
// Your code comes here
}

[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = GET”,
ResponseFormat = WebMessageFormat.Json)]
string GetData(int value);

[OperationContract]
[WebInvoke(Method = POST”,
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json)]
string[] GetUser(string Id);
}

<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name=”ServiceBehavior”>
<serviceMetadata httpGetEnabled=”true”/>
<serviceDebug includeExceptionDetailInFaults=”true”/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name=”EndpBehavior”>
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration=”ServiceBehavior” name=”Service”>
<endpoint address=” binding=”webHttpBinding”
contract=”IService” behaviorConfiguration=”EndpBehavior”/>
</service>
</services>
</system.serviceModel>

Client code with JQuery:

function CallService() {
$.ajax({
type: POST”, //GET or POST or PUT or DELETE verb
url: Service.svc/GetUser”, // Location of the service
data: {“Id”: “‘ + userid + “}’, //Data sent to server
contentType: application/json; charset=utf-8″, // content type sent to server
dataType: json”, //Expected data format from server
processdata: ProcessData, //True or False
success: function(msg) {ServiceSucceeded(msg);},
error: ServiceFailed// When Service call fails
});

function ServiceSucceeded(result) {
if (DataType == json”) {
resultObject = result.GetUserResult;

for (i = 0; i < resultObject.length; i++) {
alert(resultObject[i]);
}

}
}

function ServiceFailed(result) {
alert(Service call failed: ‘ + result.status + + result.statusText);
Type = null;
varUrl = null;
Data = null;
ContentType = null;
DataType = null;
ProcessData = null;
}

ASP.NET AJAX MaskedEditExtender

September 25, 2013
  • MaskType – Type of validation to perform:
    None – No validation
    Number – Number validation
    Date – Date validation
    Time – Time validation
    DateTime – Date and time validation
  • Mask Characters and Delimiters
    9 – Only a numeric character
    L – Only a letter
    $ – Only a letter or a space
    C – Only a custom character (case sensitive)
    A – Only a letter or a custom character
    N – Only a numeric or custom character
    ? – Any character

Linq operators on null Linq collection

September 25, 2013

You cannot apply the Linq operators (First, Single, Any, …) when the query returns null.

We have to add an extension method:

public static IEnumerable<TSource> NullToEmpty<TSource>(
    this IEnumerable<TSource> source)
{
    if (source == null)
        return Enumerable.Empty<TSource>();

    return source;
}

And now you can write:

public void Should_handle_nulls()
{
    List<int> ints = null;

    ints.NullToEmpty().Any();
}

LINQ parameter == null with SQL CE

August 6, 2013

SQL CE cannot handle parameters so, the so standard construction

.Where(p => p.Category == category || category == null)

fails because LINQ needs to use a parameter to do that value comparison in-query.  But it is perfectly valid SQL to pass something like this:

.Where(p => p.Category == category || true)

because ‘true’ is always…well…true.  So if you parse your variable to a bool before the LINQ statement and then pass that variable to SQL CE, you’re in good shape:

bool bCategory = (string.IsNullOrEmpty(category)) ? true : false;
Products = repository.Products.Where(p => p.Category == category || bCategory);

This allows you do to all the filtering in the query before returning data, and you can use this trick on as many conditions as you want without it getting messy.

Server Tags

September 24, 2012

Server Tags:

<% %> An embedded code block is server code that executes during the page’s render phase. The code in the block can execute programming statements and call functions in the current page class. http://msdn2.microsoft.com/en-gb/library/ms178135(vs.80).aspx

<%= %> most useful for displaying single pieces of information. http://msdn2.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx

<%# %> Data Binding Expression Syntax. http://msdn2.microsoft.com/en-us/library/bda9bbfx.aspx

<%$ %> ASP.NET Expression. http://msdn2.microsoft.com/en-us/library/d5bd1tad.aspx

<%@ %> Directive Syntax. http://msdn2.microsoft.com/en-us/library/xz702w3e(VS.80).aspx

<%– –%> Server-Side Comments. http://msdn2.microsoft.com/en-US/library/4acf8afk.aspx

<%: %> Like <%= %> But HtmlEncodes the output (new with Asp.Net 4). http://weblogs.asp.net/scottgu/archive/2010/04/06/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2.aspx