Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7827227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:46:22+00:00 2026-06-02T09:46:22+00:00

I am trying to write a C# restful web service which returns the customer

  • 0

I am trying to write a C# restful web service which returns the customer name when account number is passed as a parameter.

I have a customer class:

public class Customer_T
{
    public string CustName { get; set; }
}

An interface class:

[ServiceContract(Namespace = "", Name = "CustomerInfoService")]
public interface CustomerInfo_I
{
    [OperationContract]
    Customer_T CustName(string accountno);
}

*Another class called CustomerInfo which implements the CustomerInfo_I interface:*

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class CustomerInfo : CustomerInfo_I
{
    [WebInvoke(Method = "GET", UriTemplate = "customer/{accountno}", ResponseFormat = WebMessageFormat.Json)]
    public Customer_T CustName(string accountno)
    {
        string custName = "";
        CustNameByAccountNo custdb = new CustNameByAccountNo();
        custName = custdb.getCustName(accountno).ToString();
        if (custName.Equals("") == false)
        {
            return new Customer_T { CustName = custName };
        }
        else
        {
            return null; //This is where I want to change
        }
    }
}

Instead of returning null, I would like to return a string “InvalidAccNo”.
I did try it, but it gave me an error.

Cannot implicitly convert type 'string' to 'CustomerInfo.Service.Customer_T'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:46:24+00:00Added an answer on June 2, 2026 at 9:46 am

    You won’t be able to return a string since your return type is defined as a Customer_T. One option you might want to consider is to take a look at fault handling inside of WCF to return a fault to a user rather than repurposing a single object. Throwing the fault can be used in indicate that an invalid account number was specified.

    [ServiceContract(Namespace = "", Name = "CustomerInfoService")]
    public interface CustomerInfo_I
    {
        [OperationContract]
        [FaultContract(typeof(string))]
        Customer_T CustName(string accountno);
    }
    

    http://msdn.microsoft.com/en-us/library/ms733721.aspx

    Also, your Customer class, you should mark the class with the DataContract attribute and your data members with DataMember in order to return complex types.

    [DataContract]
    public class Customer_T
    {
        [DataMember]
        public string CustName { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to write a recursive function which returns the factorial of a number.
I'm trying to make a RESTful Web Service which implements all four CRUD operations.
I have a WCF restful service that I'm trying to upload an image to.
I'm trying write a query to find records which don't have a matching record
We've been trying write unit tests for a worker class written in C#, which
I am trying to write a simple (lightweight) RESTful server in Python. I have
Trying to write out syslog entries containing strings but they don't register. // person.name
Im trying to write something to get my images to show correctly. I have
Trying to write this small program to help me in my Stats class, everything
Trying to write a regex that can parse a full name and split it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.